我是一名進入軟體工程專業的學生,??正在參加我的第一堂關于 Java 編碼的計算機科學課程。我正在努力讓我的程式將一組特定條件應用于字串,以便列印有關用戶輸入是否確實是有效雙精度文字的訊息。
以下是條件:
- 完全由以下字符組成:“ ”、“-”、“.” (小數點)和“0”到“9”
- ' ' 或 '-' 字符只能作為第一個字符出現
- 這 '。' (小數點)字符必須只出現一次
- 所有其他字符必須是“0”到“9”字符
這是我的代碼:
String doubleliteral;
int a;
char j;
char k;
char l;
char m;
char n;
System.out.print("Enter a valid four character double literal: ");
doubleliteral = keyboard.nextLine();
a = doubleliteral.length();
j = doubleliteral.charAt(0);
k = doubleliteral.charAt(1);
l = doubleliteral.charAt(2);
m = doubleliteral.charAt(3);
n = '.';
char i = (char)(a 0);
if (i <= 4)
{
System.out.print("\n" doubleliteral " is a valid four character double literal.");
}
else if ((j == ' ') || (j == '-') || (j <= 9))
{
}
else if ((k <= 9))
{
}
else if (l <=9)
{
}
else if (m <= 9)
{
}
else if ((j != '.') && (k != '.') && (l != '.') && (m != '.'))
{
System.out.print("\n" doubleliteral " is not a valid four character double literal.");
}
else
{
System.out.print("\n" doubleliteral " is not a valid four character double literal.");
}
keyboard.close();
我一直在互聯網上,在我的教科書中,并通過我教授的演講進行了大約三天的搜索,現在試圖弄清楚這一點,但沒有任何結果,請幫助。
uj5u.com熱心網友回復:
這里有一些偽代碼可以讓你朝著正確的方向前進。我不能在這里寫你的學校答案。
回圈遍歷每個字符。特例第一。跟蹤是否有點。
hasDot = false
for (i=0; i<input.length; i ) {
ch = input[i]
if (i === 0) {
validChars = [0,1,2,3,4,5,6,7,8,9,-, ,.]
// validChars must contain ch
// if ch === dot, set hasDot = true
}
else {
validChars = [0,1,2,3,4,5,6,7,8,9,.]
// validChars must contain ch
// if ch === dot and hasDot then error, else set hasDot = true
}
}
uj5u.com熱心網友回復:
您需要檢查幾個條件才能確定有效性。讓我們一一看看。
或者 -
最多可以有一個 or-運算子,并且它必須位于給定輸入的開頭。
因此,您可以做的是,無論何時獲得 or-運算子,只需檢查當前索引是否0存在。
如果不是,則將輸入標記為無效。
boolean isValid = true;
for(int i = 0; i < doubleliteral.length(); i )
{
if((doubleliteral[i] == ' ' || doubleliteral[i] == '-'))
{
if(i != 0)
{
isValid = false;
}
else
{
// it's valid.
}
}
}
該.(小數點)字符
最多可以有一個.字符。
所以你要計算它,檢查它是否變得超過1。
boolean isValid = true;
int dotCounter = 0;
for(int i = 0; i < doubleliteral.length(); i )
{
if( doubleliteral[i] == '.')
{
if(dotCounter != 0)
{
isValid = false;
}
else dotCounter ;
}
}
檢查是否有除數字以外的任何字符(o 到 9)
在遍歷字串的每個字符時,您可以檢查它是否是數字,如下所示 -
for(int i = 0; i < doubleliteral.length(); i )
{
if(Character.isDigit(doubleliteral.charAt(i)))
{
// it's a digit
}
}
現在,如果我們將所有內容結合起來,它將如下所示:
boolean isValid = true;
int dotCounter = 0;
int doubleliteralLength = doubleliteral.length();
for(int i = 0; i < doubleliteralLength; i )
{
if((doubleliteral[i] == ' ' || doubleliteral[i] == '-'))
{
if(i != 0)
{
isValid = false;
break; // we don't need to check any further
}
}
else if( doubleliteral[i] == '.')
{
if(dotCounter != 0)
{
isValid = false;
break;
}
else dotCounter ;
}
else if(Character.isDigit(doubleliteral.charAt(i)) != true)
{
// it's not a digit, not or -, not .
isValid = false;
break;
}
}
if(doubleliteralLength == 4 && isValid)
{
System.out.print("\n" doubleliteral " is a valid four character double literal.");
}
else
{
System.out.print("\n" doubleliteral " is not a valid four character double literal.");
}
uj5u.com熱心網友回復:
作為一名前計算機科學教授,讓我首先說,有時 Stack Overflow 的貢獻者很難正確回答這些型別的問題,因為“正確”的答案取決于你的教授試圖教你的課程。將來,您可能希望以“我們正在嘗試學習字串決議”開始您的帖子。或類似的東西。這將有助于提供與您正在學習的課程相一致的答案。例如,通過使用正則運算式并將運算式應用于輸入的String物件,可以輕松回答此問題。盡管這個問題在技術上是正確的,但對于您教授正在教授的課程(我猜是字串決議)來說并不正確。
也就是說,這是我給你的線索:
- 符號( 或 -)只能出現在字符索引 0 處。
- 數字 0-9 的(十進制)ASCII 值為 48-57。因此,此范圍之外的 ASCII 值不是“ ”、“-”或“。” (分別為 43、45 或 46)是非法字符。
接下來您需要做的是寫下一些字符組合并手動測驗它們。例如:
- 1.34 // 無效(第一個字符必須是符號)
- .123 // 無效(第一個字符必須是符號)
- -.98 // 有效
- -0.1 // 有效
- 2.3 // 有效
- 2. 6 // 無效
- 4.3- // 無效
- 30-4 // 無效
- 23。// 有效(根據您的要求,但 IMO 不應該)
- 0.- // 無效
以上所有內容僅包含有效字符,但并非所有內容都正確格式化。測驗完這些后,您應該嘗試使用其他非數字字符。
你的代碼應該是這樣的(在psedocode中)
IF STRING LENGTH > 4, ERROR (String has too many characters)
IF PERIOD COUNT > 1, ERROR // See * note
IF CHAR AT 0 NOT VALID, ERROR (Special case for symbols, period, or numbers)
IF CHAR AT X NOT VALID, ERROR (index 1-3 can only be numbers or the period)
IF CHAR AT 3 == ., ERROR (Last index cannot be the period)
PRINT STING MESSAGE
(*) 我的建議是使用回圈操作來計算句點出現在字串上的次數。還有其他方法可以做到這一點,但我的猜測是它們與您當前的課程無關。其中一些替代方法是使用正則運算式、使用 Streams API、使用遞回或使用 3rd 方庫。遠離任何這些解決方案。
如果所有測驗都通過了,那么將列印出的唯一內容將是包含有效數字字串的訊息。
最后提示:在一次傳遞中執行所有 CHAR AT 評估(即回圈遍歷您的字串(字符陣列)以確定任何給定位置的字符是否有效。多次傳遞解決方案是可以的,只是不夠優雅或有效。
解決方案
import java.util.Scanner;
public class NumericStringEvaluator {
public static void main (String[] args) {
System.out.print("Enter a valid four character double literal: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
scanner.close();
if (input.length() > 4) {
System.err.println("String must be exactly 4 characters");
System.exit(-1);
}
//1. Consists of exactly the following characters: ’ ’, ’-’ ...
//2. Either the ’ ’ or ’-’ character may appear only as the first character
if (input.charAt(0) != ' ' && input.charAt(0) != '-') {
System.err.println("Invalid character at index 0: " input.charAt(0));
System.exit(0);
}
int periodCount = 0;
for (int i = 1; i < 4; i ) {
char c = input.charAt(i);
//1. Consists of ... ’.’ ...
if (c == '.') {
periodCount ; //3. The ’.’ (decimal point) character must appear exactly once
}
// 1. Consists of ... and ’0’ through ’9’
// 4. All other characters must be the ’0’ through ’9’ characters
// ASCII values for number are 48-57 (anything outside the range is not a number)
// and the special characters have been accounted for already
else if (c < 48 || c > 57) {
System.err.println("Invalid character at index " i ": " input.charAt(i));
System.exit(0);
}
}
// At this point, every character in the string has been validated (no errors found)
//3. The ’.’ (decimal point) character must appear exactly once
if (periodCount != 1) {
System.err.println("Decimal point appears more than once in numeric String.");
System.exit(0);
} else {
System.out.print("\n" input " is a valid four character double literal.");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335810.html
下一篇:C#:while與if
