简体   繁体   English

如何打破while循环Java

[英]How to break while loop Java

My code runs smoothly in eclipse but when I run it at codezinger , an online website to submit code it gives error such as Exception in thread "main" java.util.NoSuchElementException at this line char character = sc.next().charAt(0);我的代码在 eclipse 中运行流畅,但是当我在 codezinger 上运行它时,一个在线网站提交代码它在线程“main”java.util.NoSuchElementException 中出现异常,例如在这一行char character = sc.next().charAt( 0); at the main class .在主班。 and input mismatched at line double pay=sc.nextDouble();和输入不匹配行double pay=sc.nextDouble(); at the main class.在主班。 I really dont know why as my code runs perfectly at eclipse.我真的不知道为什么我的代码在 eclipse 上运行得很好。

this is picture of my code runs.这是我的代码运行的图片。 enter image description here在此处输入图片说明

the input at codezinger; codezinger 的输入;

5  
=  
- 0.70  
- 2.40   
- 1.20   
+ 20.00   
3  
- 5.00   
- 0.50    
=    
0

main class;主类;

import java.text.DecimalFormat;
import java.util.Scanner;
public class TestMyPrintCard {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x=1;
        DecimalFormat df = new DecimalFormat("0.00");
        int N = sc.nextInt();
        sc.nextLine();
        do {
        System.out.println("Case #" + x + ":");
        MyPrintCard c1 = new MyPrintCard(); 
        for(int i=0; i<=N; i++) {
        char character = sc.next().charAt(0); // char input =,+,- // the problem here
            
        if (character=='=' ) {
            System.out.print("RM");
            System.out.println(df.format(c1.getBalance()));
            
            
        } else if (character=='+') {        
            double topup=sc.nextDouble();
             c1.topupCard(topup);
             
            
        }else if (character=='-') {
            double pay=sc.nextDouble(); // problem  here
            c1.payService(pay);             
        }   
        }//end for  
        x++;
        } while(N!=0);  
    }
    }

the class;班上;

import java.text.DecimalFormat;
public class MyPrintCard {
    private double balance; //declare
    DecimalFormat df = new DecimalFormat("0.00");
    
    MyPrintCard(){ //constructor initialised
        balance = 10.00;        
    }
    
    //getter
    public double getBalance() {
        return balance;
    }
    
    public void topupCard(double amt) {
        
        balance += amt;
        System.out.print("RM");
        System.out.println(df.format(balance));         
    }
    
    public void payService(double amt) {    
        if(balance>5 ) {
            balance -= amt;
            System.out.print("RM");
            System.out.println( df.format(balance));                    
        }  
        else        
            System.out.println("invalid");
    }   
}

You're either lying or you're no good at testing.你要么在撒谎,要么你不擅长测试。 For example, the very example input you pasted wouldn't work, even in eclipse.例如,即使在 eclipse 中,您粘贴的示例输入也不起作用。

Your do/while loop runs forever .你的 do/while 循环永远运行 It has no stop condition at all: No break, and the while condition of N != 0 will never fail to be true , as you never modify N .它根本没有停止条件:无凉风, while条件N != 0将永远无法成为true ,因为你永远不修改N Your error is that you're supposed to re-read N at the top of that do loop, instead of before it.你的错误是你应该在do循环的顶部重新读取 N ,而不是之前 Every segment in the input starts with a number indicating how many operations follow (with 0 being used to terminate the app).输入中的每个段都以一个数字开头,指示后面有多少操作( 0用于终止应用程序)。 You only read the first such number.您只读取第一个这样的数字。

Fix that and this code will work fine... probably, there is one more small issue:解决这个问题,这段代码就可以正常工作了……可能还有一个小问题:

Separately, that sc.nextLine() is not useful to this code and dangerous in that it can mess with reading the input depending on precisely how codezinger is providing it to you.另外, sc.nextLine()对这段代码没有用处,而且危险,因为它可能会混淆读取输入,具体取决于 codezinger 为您提供的准确方式。 Note that a very large amount of tutorials on scanner and keyboard input out there, including most SO answers, erroneously suggesting you run sc.nextLine() after every input.请注意,关于扫描仪和键盘输入的大量教程,包括大多数 SO 答案,错误地建议您在每次输入后运行sc.nextLine() Do not do this 1 .不要这样做1

  1. Not relevant to this question, but I better back up such a statement: If you want keyboard input where hitting enter separates input, instead configure the scanner properly (use scanner.useDelimiter("\\\\R") , call scanner.next() to get a line of text, and don't ever call .nextLine() ).与这个问题无关,但我最好支持这样的声明:如果你想要键盘输入,其中按 Enter 分隔输入,而是正确配置扫描仪(使用scanner.useDelimiter("\\\\R") ,调用扫描仪scanner.next()获取一行文本,并且永远不要调用.nextLine() )。 This is much simpler (no need to remember to toss those pointless nextLine() in there) can read blank input, and will not fail if the user tries to separate things with spaces instead of newlines.这更简单(无需记住将那些毫无意义的nextLine()扔在那里)可以读取空白输入,并且如果用户尝试使用空格而不是换行符分隔内容,则不会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM