Java 中的分支和循环(中断和继续)

Posted

技术标签:

【中文标题】Java 中的分支和循环(中断和继续)【英文标题】:Branching and looping in Java (break and continue) 【发布时间】:2015-10-10 18:21:50 【问题描述】:

您好,我正在尝试编写一个代码,其输出类似于

Enter time in 24-hour notation:
13:07
That is the same as
1:07 PM
Again? (y/n)
y
 
Enter time in 24-hour notation:
10:15
That is the same as
10:15 AM
Again? (y/n)
y
 
Enter time in 24-hour notation:
10:65
There is no such time as 10:65
Try Again:
Enter time in 24-hour notation:
16:05
That is the same as
4:05 PM
Again? (y/n)
n
End of program

但我最终犯了一些错误,我无法弄清楚。

public class prp 
    public static void main(String[] args) 
        while (true) //add the remaining logic
        
            System.out.println("Enter time in 24-hour notation HH:MM");
            Scanner x = new Scanner(System.in);
            String newhr = x.nextLine();
            String hr[] = newhr.split(":");
            int hours = Integer.parseInt(hr[0]);//HH
            int minutes = Integer.parseInt(hr[1]);//MM

            if ((hours >= 00 && hours <= 24) && (minutes >= 00 && minutes <= 59)) 
                System.out.println("That is the same as: ");
                if (hours <= 12) 
                    System.out.println(hours + ":" + minutes + " AM");
                    //System.exit(0);
                 else if (hours > 12 && hours < 24) 
                    int hoursnew = hours - 12;
                    System.out.println(hoursnew + ":" + minutes + " PM");
                    //System.exit(0);
                
             else 
                System.out.println("There is no such time as " + hours + " : " + minutes);
                System.out.println("Try Again!");
                //continue;
            
            System.out.println("Again? [y/n]");
            Scanner y = new Scanner(System.in);
            String newyn = y.nextLine();
            if (newyn == "y" || newyn == "n") 
                if (newyn == "y") 
                    continue;
                 else 
                    System.out.println("End of program");
                    System.exit(0);
                    //break;
                
            //end of while
        
    

程序在输入非整数时显示错误。此外,它没有破裂。如果用户输入非法时间,如 10:65 或 ab:cd,我想创建另一个名为 TimeFormatException 的异常类。

【问题讨论】:

好的,但是你的问题是什么? 当我输入字符“n”时应该退出循环 ***.com/questions/2912817/… 检查此链接仅将 int 作为输入。 【参考方案1】:

实际上,在您的代码中,如果用户在询问时输入的不是 Y/N 和按时错误输入,则您没有正确比较字符串以及您没有处理您的代码。见以下代码:

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;

public class Exam 
    public static void main(String[] args) 
        while (true) // add the remaining logic
        
            System.out.println("Enter time in 24-hour notation HH:MM");
            Scanner x = new Scanner(System.in);
            String newhr = x.nextLine();
            String hr[] = newhr.split(":");
            int hours = 0;
            int minutes = 0;
            try 
                hours = Integer.parseInt(hr[0]);// HH
                minutes = Integer.parseInt(hr[1]);// MM
             catch (NumberFormatException e) 
                System.out.println("Wrong time input");
                continue;
            
            if ((hours >= 00 && hours <= 24)
                    && (minutes >= 00 && minutes <= 59)) 
                System.out.println("That is the same as: ");
                if (hours <= 12) 
                    System.out.println(hours + ":" + minutes + " AM");
                    // System.exit(0);
                 else if (hours > 12 && hours < 24) 
                    int hoursnew = hours - 12;
                    System.out.println(hoursnew + ":" + minutes + " PM");
                    // System.exit(0);
                
             else 
                System.out.println("There is no such time as " + hours + " : "
                        + minutes);
                System.out.println("Try Again!");
                // continue;
            

            while (true) 
                System.out.println("Again? [y/n]");
                Scanner y = new Scanner(System.in);
                String newyn = y.nextLine();
                if ("y".equalsIgnoreCase(newyn)) 
                    break;
                 else if ("n".equalsIgnoreCase(newyn)) 
                    System.out.println("End of program");
                    System.exit(0);
                    // break;
                 else 
                    System.out.println("Enter correct input Y or N");
                
            
            // end of while
        
    

【讨论】:

【参考方案2】:

程序有两个错误。

1.在解析成整数之前验证用户输入是否为有效数字 使用正则表达式。如果匹配返回 false 则抛出异常。

String s="235:23";//user input
System.out.println(s.matches("\\d0,2:\\d0,2"));

2.不是equalsIgnoreCase或equals,而是使用==来比较字符串

 if( "y".equalsIgnoreCase(newyn) || "n".equalsIgnoreCase(newyn))

请遵守一些编码标准!

【讨论】:

以上是关于Java 中的分支和循环(中断和继续)的主要内容,如果未能解决你的问题,请参考以下文章

举例说明c语言中的中断语句break,continue,return的区别及相同之处

do..while 循环中的继续和中断语句错误

在 Kotlin 中的功能循环中,如何“中断”或“继续”?

JAVA中的for循环运行一次后如何停止,但是还可以在输入数字后继续运行?

Java Review (流程控制)

Java Review (流程控制)