早上好, 我正在尝试自己学习一些东西。因为我已经超负荷工作了。这只是我正在从事的一个小型练习程序。我明白了这一点,但是我不确定发生了什么。 如果输入的数字不是2,则会显示“请输入有效的数字,谢谢” 当给定2时,它说“数字甚至没有,所以还有余数” 我不确定为什么要得到这个。为什么不接受其他数字,为什么说2不是偶数? 对于我所解释的错误的任何帮助,将不胜感激。谢谢。
import java.util.Scanner;
public class Assignment1
{
//Scanner keyboard = new Scanner(System.in);
//int num = keyboard.nextInt();
public static int isEven()
{
Scanner keyboard = new Scanner(System.in);
int num = keyboard.nextInt();
switch (num)
{
case 1:
if (num % 2 == 0)
System.out.println("The Number is Even no Remainders");
break;
case 2:
if (num % 2 != 0);
System.out.println("The Number is not even so there are Remainders");
break;
default:
System.out.println("Please input a valid number, Thank you.");
}//switch
/*pull number from user
//store in num
//if even print message num is even
//else print message not an even number
* This is the remainder of my psuedcode notes to remind me how my
* mind was flowing
*/
return num;
} //isEven
public static void main (String args[])
{
Assignment1.isEven();
}//main
}//public class assignment one
答案 0 :(得分:1)
开关盒的文档为here。
现在尝试一下,我在行中添加了一些注释。 仔细阅读。
public class Assignment1 {
public static int isEven() {
Scanner keyboard = new Scanner(System.in);
int num = keyboard.nextInt();
num = num % 2; // divide by 2 and get a remainder.
switch (num) {
case 0: //case 0 means if number equal to zero
System.out.println("The Number is Even no Remainders");
break;
case 1: // case 1 means if number equal to one
System.out.println("The Number is not even so there are Remainders");
break;
default: // if no one match if not a valid.
System.out.println("Please input a valid number, Thank you.");
}//switch
/*pull number from user
//store in num
//if even print message num is even
//else print message not an even number
* This is the remainder of my psuedcode notes to remind me how my
* mind was flowing
*/
return num;
} //isEven
public static void main(String args[]) {
Assignment1.isEven();
}//
}