[英]While loop not repeating in java
A program that takes ticket orders and provides the total. 获取订单并提供总额的程序。 It's not letting me answer the question whether, i would like to buy more tickets. 这不是让我回答这个问题,我是否想购买更多门票。 I'm a beginner, If you guys could guide me or suggest any upgrades that would be helpful. 我是初学者,如果你们可以指导我或建议任何有用的升级。
package ridecalc;
import java.util.Scanner;
/** *
1. @author */
public class Ridecalc {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner(System.in);
double tickets = 0;
double totprice;
String moretickets = "";
System.out.println("Hi! welcome to JP's amusment park");
do {
if (response()) {
System.out.println("How many tickets would you like to purchase ?");
tickets = in.nextInt();
} else {
totprice = 0;
}
totprice = calc(tickets);
System.out.println("The total amount for the tickets are :" + totprice);
System.out.println("Would you like to buy more tickets ?(y/n)");
moretickets = in.nextLine();
} while (moretickets.equalsIgnoreCase("y"));
}
public static boolean response() {
Scanner in = new Scanner(System.in);
String response = "";
System.out.println("Would you like to buy any tickets (y/n)");
response = in.nextLine();
if (response.equalsIgnoreCase("y")) {
return true;
} else {
return false;
}
}
public static double calc(double tickets) {
double totprice;
totprice = (tickets * 20);
return totprice;
}
}
List item 项目清单
Firstly, your response.equalsIgnoreCase("y")
never evaluates to true because it is always an empty string, there has been no affectation to the variable response
. 首先,您的response.equalsIgnoreCase("y")
永远不会计算为true,因为它始终是一个空字符串,对变量response
没有任何影响。 So I added a boolean rps
that will store the response of the user from your function response()
and loop until it is true. 所以我添加了一个boolean rps
,它将从函数response()
存储用户的response()
并循环直到它为真。
EDIT: Fixed usage of Scanner, only one Scanner instance EDIT 2: Fixed double condition 编辑:修复了扫描仪的使用,只有一个扫描仪实例 编辑2:固定双重条件
package ridecalc;
import java.util.Scanner;
/** *
1. @author */
public class some {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner(System.in);
double tickets = 0;
double totprice;
String moretickets = "";
System.out.println("Hi! welcome to JP's amusment park");
boolean rps = true;
while( rps ){
if ( (rps = response(in)) == true) {
System.out.println("How many tickets would you like to purchase ?");
tickets = in .nextInt();
} else {
totprice = 0;
}
totprice = calc(tickets);
System.out.println("The total amount for the tickets are :" + totprice);
System.out.println("Would you like to buy more tickets ?(y/n)"); // this doesn't seem necessary since there is no test following
in.next();
moretickets = in.nextLine();
}
}
public static boolean response(Scanner in) {
String response = "";
System.out.println("Would you like to buy any tickets (y/n)");
response = in.nextLine();
if (response.equalsIgnoreCase("y")) {
System.err.println("here");
return true;
} else {
return false;
}
}
public static double calc(double tickets) {
double totprice;
totprice = (tickets * 20);
return totprice;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.