当前位置: 首页 > 知识库问答 >
问题:

仅允许java scanner允许数字

宫子晋
2023-03-14

我一直在捕捉非数字时遇到问题。

我试过了,但抓不住。如果我让它捕获非数字,但不让用户再次尝试输入。。。它完全停止了我的代码。

这是我的密码:

package triangle;

import java.util.Scanner;

public class traingle {

public static void main(String[] args){ 
    //explaining what the program is going to do 
System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\nthis automated program will tell you if the angle you entered is issosoliese, eqilateral or scalene");
//creating input 1,2,3 for user

Scanner angle1 = new Scanner(System.in);  // Reading from System.in
System.out.println("Enter angle degree number 1: ");
int n = angle1.nextInt();

Scanner angel2 = new Scanner(System.in);
System.out.println("Enter angle degree number 2: ");
int n2 = angel2.nextInt();

Scanner angel3 = new Scanner(System.in);
System.out.println("Enter angle degree number 3: ");
int n3 = angel3.nextInt();
//this is just telling how much degrees the user had in total if they didnt match up to triangle standards

共有2个答案

轩辕乐邦
2023-03-14

你有一个简单的问题。。您只需对main方法中的代码使用一个扫描器。

 package triangle;

 import java.util.Scanner;

 public class traingle {

 public static void main(String[] argc){
 System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\nthis automated program will tell you if the angle you entered is issosoliese, eqilateral or scalene");

 Scanner angle1 = new Scanner(System.in);  

 System.out.println("Enter angle degree number 1: ");

 int n = angle1.nextInt();

 System.out.println("Enter angle degree number 2: ");

int n2 = angel2.nextInt();

System.out.println("Enter angle degree number 3: ");

int n3 = angel3.nextInt();
姬宝
2023-03-14

这应该让你开始。

package triangle;

import java.util.Scanner;

public class triangle {

    public static void main(String[] args){ 
         //explaining what the program is going to do 
        System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\n");
        System.out.println("this automated program will tell you if the angle you entered is isoceles, eqilateral or scalene");
        //creating input 1,2,3 for user

        Scanner s = new Scanner(System.in);  // Reading from System.in

        String line;
        int [] angles = new int [3];
        int count = 0;

        do {
            System.out.print("Enter angle degree number " + (count+1) + ": ");
            line = s.nextLine();
            while (true ) {
                try {
                    angles[count] = Integer.parseInt(line);
                    System.out.println("You entered " + angles[count]);
                    count++;
                    break;
                } catch (NumberFormatException e ) {
                    System.out.println("Invalid number: try again: ");
                    line = s.nextLine();
                }
            }
        } while (count < 3);


    }
}
 类似资料:
  • 问题内容: 我在这里的研究中得到了不同的答案。 有人可以验证Redis服务器只能存储任何数值的表示吗? 例如,如果我在lpush中使用具有双重类型的Java客户端(Jedis),在发送给Redis之前是否需要将其转换为等效于字符串类型的内容? 还是有一种方法可以发送诸如double之类的实际数字类型?如果是这样,是否有任何示例代码说明了如何实现此目的? 谢谢 问题答案: Redis将所有内容存储在

  • 问题内容: 如何只允许本地请求进行Elasticsearch?所以命令像: 只能在localhost上运行,并要求类似: 会被拒绝? 因为从我看来,elasticsearch默认情况下允许它。 编辑: 根据http://www.elasticsearch.org/guide/reference/modules/network.html, 您可以管理参数以允许主机。默认情况下,它设置为 问题答案:

  • 问题内容: 我看到以下错误: 使用此代码: 是什么原因引起的,如何解决? 问题答案: 在当前域之外发出ajax请求时,Javascript是受限制的。 例1:您的域名为example.com,并且您想向test.com提出请求=>您不能。 例2:您的域名是example.com,并且您想向inner.example.com发送请求,但是您不能。 例3:您的域名为example.com:80,并且您

  • 问题内容: 如何允许我在iOS 8.3 SDK中用Swift编写的通用应用程序在iPhone上仅支持纵向模式,而在iPad上既支持纵向模式又支持横向模式? 我知道过去这是在AppDelegate中完成的。我该如何在Swift中做到这一点? 问题答案: 您可以以编程方式进行操作,或者更好的是,您可以简单地编辑项目的Info.plist(这应该更实用,因为它是全局设备配置) 只需添加“支持的界面方向(

  • 问题内容: 我需要找到一个仅允许字母数字的reg ex。到目前为止,我尝试的每个人都只能在字符串是字母数字的情况下工作,这意味着同时包含字母和数字。我只想要一个允许任何一个但不需要两个都允许的东西。 问题答案: /^[a-z0-9]+$/i 更新(支持通用字符) 如果您需要此正则表达式支持通用字符,则可以找到unicode字符列表。 例如:

  • 问题内容: 有没有一种快速的方法来将HTML文本输入()设置为仅允许数字键击(加’。’)? 问题答案: 注意: 这是更新的答案。下面的注释指的是一个旧版本,其中充斥着密钥代码。 JavaScript 您可以使用以下功能过滤文本的输入值(支持CopyPaste,Drag+Drop,键盘快捷键,上下文菜单操作,不可键入的键,插入标记的位置,不同的键盘布局以及IE9以后的所有浏览器 : 现在,您可以使用