Java事件处理循环输入? [英] Java Event Handling loop Input?

查看:129
本文介绍了Java事件处理循环输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的用于捕获输入错误的程序。捕获工作正常但是当我使用do时说用户如果要继续按'y'或完成此按'n'但是当用户输入任何字符时'当我调试这个调试器忽略字符输入时,它会继续相同。

这是代码。

The Program which i created to catch the input error.Catch works Fine but when i use do while to say user if you want to continue press 'y' or finish this press 'n' but when user enter any character expect 'n' it continue the same.when i debug this debugger ignore character input.
This is the Code.

import java.util.Scanner;
import java.util.InputMismatchException;
public class Asd {

    public static void main(String[] args) {
        int numeator;
        int denominator;
        double result;
        Scanner s = new Scanner(System.in);
          
        char m;
        do {  
        	  
            try{
            
                System.out.print("Enter Numenator:");
                numeator=s.nextInt();
                		
                System.out.print("Enter Denominator:");
                denominator=s.nextInt();
                		
                result=numeator/denominator;
                		
                System.out.println("Answer: "+result);
            }
            catch(InputMismatchException e){
                System.out.println("error=> must enter integer values");
            }
            catch(ArithmeticException e){
                System.out.println("error=> falseairthmtic");
            
            }
            System.out.println("If yes prees 'y' or no 'n'");
            m=s.next().charAt(0);
        } while(m!='n');
    
    }
}



我想解决这个问题,当用户按'y'然后继续相同的程序。任何其他字符给出相同的message.or'n'退出


I want to fix this problem when user press 'y' then it continue the same procedure.At any other characters it give same message.or'n' for exit

推荐答案

你不检查用户是否按下了'y',只有当他/她做了不按'n'。因此除'n'之外的任何字符都将继续循环。你需要添加另一个循环来检查按下哪个字符,例如:

You do not check to see if the user pressed 'y', only if he/she did not press 'n'. So any character other than 'n' will continue the loop. You need to add another loop to check which character is pressed, something like:
    do {
        System.out.println("If yes press 'y' or no 'n'");
        m=s.next().charAt(0);
        if (m == 'y' || m == 'n')
            break;
    } while (1);
} while(m == 'y');


这篇关于Java事件处理循环输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆