# 请教一个判断a值为正负的程序（很基础的，别笑我）~

cmcmone 2006-06-02 10:44:32

public class ACost {
int a;
public static void main(String[] args) {
if (a > 0) {
System.out.println("a是正数");
}
else {
if (a < 0) {
System.out.println("a是负数");
}
else {
System.out.println("a为零");
}
}
}
}

...全文
136 点赞 收藏 9

9 条回复

rw99yy 2006-06-02
System.out.println(a > 0? "正数":"负数");

cmcmone 2006-06-02

cmcmone 2006-06-02

xxedge 2006-06-02

byte b[]=new byte[30];
int n;
//remove last two byte for 0x0A 0x0D
int a=Integer.parseInt(new String(b,0,n-2));

cmcmone 2006-06-02

cyxlsm 2006-06-02
import java.io.IOException;

/**
*
*/

/**
* @author ly
*
*/
public class ACost {
int a;
public static void main(String[] args) throws NumberFormatException, IOException {
int a;
if (a > 0) {
System.out.println("a是正数");
}
else {
if (a < 0) {
System.out.println("a是负数");
}
else {
System.out.println("a为零");
}
}
}
}

cyxlsm 2006-06-02
public class ACost {
int a;
public static void main(String[] args) {
if (a > 0) {
System.out.println("a是正数");
}
else {
if (a < 0) {
System.out.println("a是负数");
}
else {
System.out.println("a为零");
}
}
}
}

Voxer 2006-06-02

import java.io.*;
import java.lang.*;
public class ACost {

public static void main(String[] args) {
try{
System.out.print("请输入数字a：");
while(input.length()>0&& !isInt(input)){
System.out.print("请输入数字a：");
}
int a = Integer.parseInt(input);
if (a > 0) {
System.out.println("a是正数");
}
else {
if (a < 0) {
System.out.println("a是负数");
}
else {
System.out.println("a为零");
}
}
}catch(Exception e){
}
}
//判断输入字符串是否为数字
private static boolean isInt(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}

ms_ms 2006-06-02

Java SE

6.2w+

Java 2 Standard Edition

2006-06-02 10:44