下面是一个java程序来猜测汤姆有多少个苹果。如果汤姆的苹果数量比猜测的数量多,汤姆就会打得更高,如果它更少,他就会打低。当猜到的数字正确时,他会回答没有。我的任务是为这个程序编写一个 JUnit 测试用例。由于基于用户输入,猜测的数字要么递减要么递增,直到我们达到实际值,这让我对如何为该程序编写 JUnit 测试用例感到困惑
public static void main(String args[]){
System.out.println("Guess the number of Apples Tom has");
Scanner sc= new Scanner(System.in);
int number=sc.nextInt();
System.out.println("Tom, is it higher or lower?");
String higherOrLower=sc.nextLine();
while(true){
number= getValue(higherOrLower,number);
System.out.println("Tom, is it higher or lower?");
higherOrLower=sc.nextLine();
if(higherOrLower.equalsIgnoreCase("none")) {
break;
}
}
}
public static int getValue(String i,int j){
if(i.equalsIgnoreCase("lower")) {
j--;
return j;
} else if(i.equalsIgnoreCase("higher")) {
j++;
return j;
} else {
return j;
}
}