我编写了一部分代码来获取用户输入,将其与字符串值匹配,然后使用相关的double值进行计算:
double [] currency = new double[] {0.05,0.10,0.20,0.50,1.00,2.00,5.00,10.00,20.00,50.00,100.00};
String [] currencytext = {"$0.05","$0.10","$0.20","$0.50","$1.00","$2.00","$5.00","$10.00","$20.00","$50.00","$100.00"};
Scanner keyboard = new Scanner(System.in);
for (int i = 0; i < currencytext.length; i++) {
boolean valid = false;
while(!valid){
System.out.format("$%.2f remains to be paid. Enter coin or note: ",sum);
String payment = keyboard.next();
if(payment.equals(currencytext[i])){
sum = sum - currency[i];
if(sum == 0) {
System.out.print("You gave " + payment);
System.out.print("Perfect! No change given.");
System.out.print("");
System.out.print("Thank you" + name + ".");
System.out.print("See you next time.");
}
}
if(!(payment.equals(currencytext[i]))) {
System.out.print("Invalid coin or note. Try again. \n");
}
if(payment.equals(currencytext[i]) && currency[i] > sum){
System.out.print("You gave " + payment);
System.out.print("Your change:");
}
}
}
问题在于,当它获得用户输入时,除了$ 0.05之外,它不匹配任何字符串值。在我看来,它不是正确地迭代数组,但我无法弄清楚原因。有人能在这里看到问题吗?
答案 0 :(得分:0)
当currencytext[i]
与payment
不匹配时,它会执行以下代码:
System.out.print("Invalid coin or note. Try again. \n");
System.out.format("$%.2f remains to be paid. Enter coin or note: ",sum);
payment = keyboard.next();
因此,它会在所有执行此操作时输入与currencytext[i]
不匹配的次数。
而且,在这个区块中,你有
payment = keyboard.next();
因此,它要求在此块本身中输入新输入。因此,您将获得除$0.05
之外的所有输入的所述输出。
就$0.05
而言,您的第一个if
块成功执行,并且不打印任何输出。因此,它移动到while循环的下一次迭代,同样payment
保持不变($0.05
),但currencytext[i]
变为$0.10
。所以他们不匹配,你得到了所说的输出。
如何解决此问题:
使用此代码,您需要执行批次更正。
我建议你再从头开始。
答案 1 :(得分:0)
如果它不合适,则将有效设置为true,因此代码有机会检查@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
ourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand1.getText().toString().trim().length() > 0) {
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
} else {
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
}
}
});
}
处的第一项currencytext[0]
。然后$0.05
也是如此,您的代码在那里打印行。您的!payment.equals(currencytext[i])
也未正确嵌套。
答案 2 :(得分:0)
这是您的问题的可能解决方案
Scanner keyboard = new Scanner(System.in);
double [] currency = new double[] {0.05,0.10,0.20,0.50,1.00,2.00,5.00,10.00,20.00,50.00,100.00};
String [] currencytext = {"$0.05","$0.10","$0.20","$0.50","$1.00","$2.00","$5.00","$10.00","$20.00","$50.00","$100.00"};
String payment = keyboard.next();
double sum = 100; // <- Working example - Read sum from keyboard entry
while (sum > 0) {
boolean paymentFound = false;
for (int i = 0; i < currencytext.length; i++) {
if (payment.equals(currencytext[i])) {
sum = sum - currency[i];
paymentFound = true;
if (sum == 0) {
System.out.println("You gave " + payment);
System.out.println("Perfect! No change given.");
// System.out.print("Thank you" + name + ".");
System.out.println("See you next time.");
break;
} else if (sum < 0) {
System.out.println("You gave " + payment);
System.out.println("Your change:" + (-1 * sum));
break;
}
}
}
if (!paymentFound) {
System.out.println("Invalid coin or note. Try again. \n");
}
if (sum > 0) {
System.out.format("$%.2f remains to be paid. Enter coin or note: ", sum);
payment = keyboard.next();
}
}
while循环将继续,直到付款已满。
for循环遍历数组,直到找到合适的付款
sum
中扣除。在这两种情况下,我们使用break
退出for循环。没有必要继续搜索。如果找不到合适的付款[!paymentFound
],我们会继续询问。
if (!paymentFound) {
System.out.println("Invalid coin or note. Try again. \n");
}
if (sum > 0) {
System.out.format("$%.2f remains to be paid. Enter coin or note: ", sum);
payment = keyboard.next();
}
程序将在(sum&lt; 0)结束时结束,在这种情况下,while循环退出。
我使用println
代替print
来改善邮件易读性。
答案 3 :(得分:-2)
我不知道你是如何阅读输入的。您可以做的一项改进是在for循环中写入读取输入代码。
Scanner scanner = new Scanner(System.in);
for (... ) {
....
String payment = scanner.nextLine();
....
}