求解两道JAVA题目(基础)

因为我们刚开始学JAVA没多久,老师说不能用(if)来写,让我们利用System.out.println(Integer.signum(-3)+""+Integer.si... 因为我们刚开始学JAVA 没多久 ,老师说不能用(if)来写,让我们利用 System . out . println ( Integer . signum (-3)+" "+ Integer . signum (100));这个方法来做负数和正数。题目大概意思是:让我们输入一些整数,如果最后的数字式0就结束,然后统计输入的这些整数中: 负数的有几个,正数的有几个,这些整数加起来的和,还有平均值。 PS:我不懂的怎样输入一些整数,然后再读取他们做统计。

这题老师给了俩提示:①String line = scanner . nextLine ();②检查字符C是否是一个十六进制数字:('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
展开
 我来答
匿名用户
推荐于2016-12-01
展开全部
//第一题
import java.util.Scanner;

public class DATAENTRY {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int positive = 0;
int negative = 0;
int total = 0;
int difference = 0;
double sum = 0;
System.out.print("Enter an integer, the input ends if it is 0:");
int num = cin.nextInt();
while(num!=0){
difference += Integer.signum(num);
++total;
sum += num;
num = cin.nextInt();
}

positive  = (total+difference)>>>1 ;
negative  = (total-difference)>>>1 ;
System.out.println("The number of positive is "+positive);
System.out.println("The number of negative is "+negative);
System.out.println("The total is "+(total+1));
System.out.println("The average is "+sum/total);
}
}

//第二题
public class DATAENTRY {
static boolean isHexadecimal(char c){
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String input  = cin.nextLine();
char[] charArr = input.toCharArray();
StringBuilder builder = new StringBuilder();
int i = 0;
while(i<charArr.length&&!isHexadecimal(charArr[i++]));
i-=1;
if(i < charArr.length){
while(i<charArr.length&&isHexadecimal(charArr[i])){
builder.append(charArr[i]);
++i;
}
}
System.out.println("The first hexadecimal number is \""+builder.toString()+"\"");
}
}
追问
亲,你好厉害啊!下面那题你会吗?
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式