日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java不等于正整数怎么输入_关于java:测试输入是否为正整数

發布時間:2023/12/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java不等于正整数怎么输入_关于java:测试输入是否为正整数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本問題已經有最佳答案,請猛點這里訪問。

好的,我似乎無法找到解決問題的明確答案。

我需要一個Java程序,該程序可以返回用戶輸入的值,即正整數。

為了將此與許多其他類似的問題區分開,我需要進行編程以使輸入字符串時不會崩潰。該程序將需要重復提示用戶輸入,直到輸入正整數為止。該腳本將是這樣的:

Input: 7.2

Error, try again: -5

Error, try again: -2.4

Error, try again: 3.77777

Error, try again: -1

Error, try again: 0

Error, try again: -33

Error, try again: microwave

Error, try again: -1

Error, try again: 4.2

Error, try again: hello

Error, try again: 3.14159

Error, try again: 4

4 is a positive integer.

重要的是,輸入類似單詞的程序時不要崩潰。我不知道該程序執行這種操作的方法。我可以在掃描器中使用hasNextInt,但是隨后我不知道如何檢查它是否大于零,因為在另一種情況下輸入可能是字符串。

有人可以幫我解決一個返回上述腳本的程序嗎?非常感謝。

編輯:好的,我終于在另一個問題中找到了想要的答案。此解決方案不使用parseInt(),也不使用我一直在尋找的try / catch。漂亮又簡單。所以這是解決方案代碼:

Scanner in = new Scanner(System.in);

int num;

System.out.print("Input:");

do {

while (!in.hasNextInt()) {

System.out.print("Error, not integer. Try again:");

in.next();

}

num = in.nextInt();

if (num<=0){

System.out.print("Error, not positive. Try again:");

}

} while (num <= 0);

System.out.println(num +" is a positive integer.");

讀取nextLine,使用try-catch并解析為Integer,捕獲異常。 如果沒有發生異常,請檢查num<0?

@KevinEsche對try-catch不太熟悉,您能詳細說明一下如何在代碼中使用它嗎? 對不起,我不是很聰明:(

嘗試這種方式:

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int value = -1;

do {

System.out.print("Input:");

String input = scanner.next();

try {

value = Integer.parseInt(input);

} catch (NumberFormatException e) {

System.out.println("Error, try again:" + input);

}

} while (value < 1);

System.out.println(value +" is a positive integer.");

}

我需要學習更多java :(但謝謝您的工作!

現在它是可執行的,并且接近您的要求。 是的,學習Java!

以下是兩種情況:

方案1:

-我假設正整數是指整數1,2,3,4等...

-不會將諸如4.00之類的值視為整數

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int value = 0;

System.out.print("Input:");

while (value < 1){

String input = scanner.next();

try {

value = Integer.parseInt(input);

if(value < 1){

System.out.print("Error, try again:");}

} catch (NumberFormatException e) {

System.out.print("Error, try again:");

}

}

System.out.println(value +" is a positive integer.");

}

}

方案2:

-我假設正整數是指整數1,2,3,4等...

-如4.00之類的值將被視為整數

import java.math.BigDecimal;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Input:");

String value ="";

boolean positiveIntegerNoDecimals = false;

while (positiveIntegerNoDecimals == false) {

value = scanner.next();

positiveIntegerNoDecimals = isPositiveIntegerNoDecimals(value);

if (positiveIntegerNoDecimals == false)

System.out.print("Error, try again:");

}

System.out.println(value +" is a positive integer.");

}

private static boolean isPositiveIntegerNoDecimals(String input) {

try {

int value = -1;

BigDecimal decimal = new BigDecimal(Double.parseDouble(input));

int scale = decimal.scale();

if (scale == 0)

value = decimal.stripTrailingZeros().intValue();

if (value > 0)

return true;

} catch (NumberFormatException e) {}

return false;

}

}

您需要以某種方式驗證輸入。如果輸入的是Integer。那么您需要將其打印到屏幕上,如果輸入的是Long,則需要打印錯誤消息Try again。

在下面的示例中,我創建了一個方法,該方法是C# TryParse方法的派生類。 C#TryParseInt

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

while (true)

{

@SuppressWarnings("resource")

Scanner reader = new Scanner(System.in);

System.out.println("Enter a number:");

String input = reader.next();

tryParseInt(input);

}

}

private static void tryParseInt(String input) {

try {

int val = Integer.parseInt(input);

System.out.println(val +" is a positive Integer");

} catch (NumberFormatException e) {

System.out.println(input +" Is not a number/positive Integer," + e.getMessage());

}

}

}

這樣做是將輸入解析為Integer,如果由于輸入是String或不是有效的正數Integer而失敗,則會將其解析為NumberFormatException。然后,我可以打印所需的錯誤消息。

祝好運!

這是指向Java中異常處理的鏈接。通常,您只需要將代碼放入try塊即可。然后,如果在該塊中發生預期的異常,您的程序將在catch塊中執行代碼并繼續工作。

該鏈接非常有用,謝謝!

Integer.parseInt(String s)

就是為此而寫的。

不熟悉該方法,您是否可以向我展示其在程序中的使用方式?

告訴我們一些使用掃描儀的代碼? 病態指南將指導您在何處以及如何使用它。

總結

以上是生活随笔為你收集整理的java不等于正整数怎么输入_关于java:测试输入是否为正整数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。