package prog;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Scanner;
public class assign {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Which type of ticket do you require stand or terrace?");
char tickType=sc.next().charAt(0);
System.out.println("How many tickets do you require?");
byte amount=sc.nextByte();
if (amount<5)
System.out.println("How many tickets for students?");
byte stud=sc.nextByte();
if (amount>=5)
System.out.println("You are entitled to a 12% discount.");
else if (stud>0)
System.out.println("discount will be 10%");
else
System.out.println("You are not entitled to a discount");
if (tickType=='S'||tickType=='s')
System.out.println("Ticket cost is\t "+amount*20.00);
else
System.out.println("Ticket cost is\t"+(amount*15.00));
double stand= amount*20.00;
double terrace= amount*15.00;
//VAT is 23%
当我到达
System.out.println("How many tickets do you require?");
byte amount=sc.nextByte();
如果我输入5或更高的数字,我必须输入两次而不是一次,有人可以告诉我为什么会这样吗?
答案 0 :(得分:2)
你的逻辑有一个缺陷:如果金额小于5,那么你打印“学生票数多少?”,但是即使金额大于或等于5,也要读取值 。
最好的解决方案是在byte stud = 0;
之前声明if (amount < 5)
,然后更正if语句以包含两行:
byte stud = 0;
if (amount < 5) {
System.out.println(...);
stud = sc.nextByte()
}
这样,只有当金额小于5时,才会读取螺柱的值。
答案 1 :(得分:0)
您必须输入大于5的第一个值。在这种情况下,它不会传递条件if(amount<5)
因此不会打印s.o.p并期望输入,因为下一行是byte stud=sc.nextByte();