在java程序中输入两位数作为输入后显示一位数
在下面的java代码中,如果我输入两位数作为输入,那么输入的值是一位数。我认为问题在于i&num变量的数据类型不同。 2.如果我将num作为int,则输入的值将转换为不同的值。 就像我输入了20&它取了50。 请帮忙在java程序中输入两位数作为输入后显示一位数,java,Java,在下面的java代码中,如果我输入两位数作为输入,那么输入的值是一位数。我认为问题在于i&num变量的数据类型不同。 2.如果我将num作为int,则输入的值将转换为不同的值。 就像我输入了20&它取了50。 请帮忙 class primenumber { public static void main(String args[]) throws java.io.IOException{ System.out.println("enter the number to t
class primenumber {
public static void main(String args[])
throws java.io.IOException{
System.out.println("enter the number to test for prime number");
char num;
num=(char)System.in.read();
System.out.println("number entered is "+num);
boolean tarun =true;
for(int i=2;i<=num/i;i++){
if(num%i==0){
tarun=false;
break;
}
}
if(tarun)
System.out.println(num +" prime number");
else
System.out.println(num +" is not prime number ");
}
}
`输出:-
输入要测试素数的数字
22
输入的数字是2
2不是质数
num=(char)System.in.read();
你正在将数字转换为角色?你为什么要这样做
因为num是一个。。。号码,请执行以下操作:
int num;
num = System.in.read();
char将只接受一个字符。因此,字符中的22将是2,它将跳过第二段。尝试Integer.parseIntSystem.in.read 您要将其转换为字符?试着投双人,因为你想要双人否则,只需在integer.parseIntConsole.readLine中将其保留为整数 System.in.read only读取单个字符。请参阅Scanner.nextIntBtw,因为您是新来的,您应该会看到以下教程: