基本上,我尝试使用switch但是当我运行它时,它没有做我制作的System.out.println。它编译没有问题。我不擅长编程,所以请解释一下你的答案。
import java.util.*;
public class MrCoffee {
public static void main(String[] args) {
String a;
char d = 'y';
Scanner mock = new Scanner(System.in);
System.out.println("Hi, I am Mr.Coffee what is your name?");
a = mock.nextLine();
System.out.println("Thank you " + a + ".");
System.out.println(a + ", do you have a validate rewards card currrently with you?");
String b = ("y"), c = ("n");
System.out.println("If yes enter the letter y. If no, please enter the letter n.");
d = mock.next().charAt(0);
if (d == 'y') {
Scanner low = new Scanner(System.in);
System.out.println("Ok, " + a + " please enter the amount of coffee you have bought today.");
int coffeeam, none, one, two, three, value = 1, value2 = 4;
coffeeam = low.nextInt();
double total = coffeeam * 2.85;
String start = ("Thank you Alexander, you will recieve"), rat = ("The total due amount is");
none = coffeeam / 20;
switch (none) {
case 1:
System.out.println("Unfortunately " + a + ", this is not enough to recieve any free coffee.");
break;
case 2:
System.out.println("Unfortunately " + a + ", this is not enough to recieve any free coffee.");
break;
case 3:
System.out.println("Unfortunately " + a + ", this is not enough to recieve any free coffee.");
break;
case 4:
System.out.println("Unfortunately " + a + ", this is not enough to recieve any free coffee.");
break;
case 5:
System.out.println(start + " 1 free coffee. " + rat + " " + total + " dollars.");
break;
case 6:
System.out.println(start + " 1 free coffee. " + rat + " " + total + " dollars.");
break;
case 7:
System.out.println(start + " 1 free coffee. " + rat + " " + total + " dollars.");
break;
case 8:
System.out.println(start + " 1 free coffee. " + rat + " " + total + " dollars.");
break;
case 9:
System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars.");
break;
case 10:
System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars.");
break;
case 11:
System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars.");
break;
case 12:
System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars.");
break;
case 13:
System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars.");
break;
case 14:
System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars.");
break;
case 15:
System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars.");
break;
case 16:
System.out.println(start + " 3 free coffee. " + rat + " " + total + " dollars.");
break;
case 17:
System.out.println(start + " 3 free coffee. " + rat + " " + total + " dollars.");
break;
case 18:
System.out.println(start + " 3 free coffee. " + rat + " " + total + " dollars.");
break;
case 19:
System.out.println(start + " 3 free coffee. " + rat + " " + total + " dollars.");
break;
case 20:
System.out.println(start + " 3 free coffee. " + rat + " " + total + " dollars.");
break;
}
} else if (d == 'n') {
System.out.println("Sorry " + a + ", since you do not have a rewards card. I will not be able to decide on the number of free coffee you can get.");
System.out.println("I would reccomend that you go to speak with one of our employees to register for a card.");
}
}
}
答案 0 :(得分:1)
好的问题是因为你要用整数除以整数:
none = coffeeam / 20;
然后,如果玩家输入任何低于20的值,例如coffeeam = 19,则没有任何值为0,因为它将其四舍五入为0.因此,您的切换未运行,因为大部分时间都是无值我不知道为什么你把它除以20,但如果你能详细说明为什么除以20,我可以提供一个替代解决方案,以避免整数的舍入问题。
此外,我还想补充一点,你的开关看起来效率很低。如果您指定一个String变量并且只运行一个System.out.println方法,例如:
,则会更好String message;
switch (variable) {
case 1:
message = "Cat";
break;
case 2:
message = "Dog";
break;
default:
message = "None";
break;
}
System.out.println(message);