下面是代碼。為了修復這個錯誤,我簡單地將getFraction()方法中的代碼重寫為den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/") 1, fracValue.length())),通過添加 1. 我在課程中從未見過或學習過這個,我只是在做專案時遇到過這個。我想了解代碼在num = Integer.parseInt(fracValue.substring(0, fracValue.indexOf("/")))and 中做了什么den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/"), fracValue.length())),我們正在將分子轉換為 int,num 是 之前的所有值/,den 是/.之后的所有值。我對嗎 ?我的第二個問題是為什么我們需要 1在indexOf("/")? 是不是我們在 之后取值/?
import java.util.Scanner;
public class FractionCalculator {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
intro();
while (true) {
String operation = getOperation();
Fraction frac1 = getFraction();
Fraction frac2 = getFraction();
Fraction result = new Fraction(1,1);
String result2 = "";
if (operation.equals("=")) {
System.out.println(frac1 " " operation " " frac2 " is " frac1.equals(frac2));
} else {
if (operation.equals(" ")) {
result=frac1.add(frac2);
} else if (operation.equals("-")) {
result=frac1.subtract(frac2);
} else if (operation.equals("/")) {
if(frac2.getNumerator()==0) {
result2="Undefined";
} else {
result=frac1.divide(frac2);
}
} else if (operation.equals("*")) {
if(frac2.getNumerator()==0) {
result2 = "Undefined";
} else {
result=frac1.multiply(frac2);
}
}
//print results
} if (result2!="") {// division and multiplication by zero is undefined
System.out.println(frac1 " " operation " " "0" " = " result2);
} else if (result.getNumerator()%result.getDenominator() == 0) {
System.out.println(frac1 " " operation " " frac2 " = " (result.getNumerator()/ result.getDenominator()));
} else {
System.out.println(frac1 " " operation " " frac2 " = " result.toString());
}
}
}
public static void intro() {
System.out.println("\nThis program is a fraction calculator");
System.out.println("It will add, subtract, multiply and divide fractions until you type Q to quit.");
System.out.println("Please enter your fraction in the form a/b, where a and b are integers.");
for (int i=0; i<80; i ) {
System.out.print("-");
}
}
public static String getOperation() {
System.out.println("\nPlease enter an operation ( , -, /, *, = or \"Q\" to quit): ");
Scanner input = new Scanner(System.in);
String operation = input.nextLine();
int x = 0;
while (x == 0) {
if (operation.equals(" ") || operation.equals("-") || operation.equals("/") || operation.equals("*") || operation.equals("=")) {
x ;
} else if (operation.equalsIgnoreCase("q")) {
System.exit(0);
} else {
System.out.println("Invalid input, enter valid operation ( , -, /, *, = or \"Q\" to quit)");
operation = input.nextLine();
}
}
return operation;
}
public static boolean validFraction(String input) {
boolean valid;
if (input.startsWith("-")) {
input = input.replaceFirst("-",""); // or use 'input.substring("1", input.length())';
}
if (input.contains("-") || input.charAt(input.indexOf("/") 1)==('0') || input.contains(" ")) {
valid = false;
} else if (input.contains("/")) {
input = input.replace("/", "");
}
if (input.matches("^[0-9] $") && input.length() > 0) {
valid = true;
} else {
valid = false;
}
return valid;
}
public static Fraction getFraction() {
System.out.println("Please enter a fraction (a/b) or integer (a): ");
String fracValue = input.nextLine();
//validate input
while (!validFraction(fracValue)) {
System.out.println("Please enter a fraction (a/b) or integer (a): ");
fracValue = input.nextLine();
}
//convert to numerator, denominator
int num = 0;
int den = 0;
if (fracValue.contains("/")) {
num = Integer.parseInt(fracValue.substring(0, fracValue.indexOf("/")));
den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/"), fracValue.length()));
} else {
num = Integer.parseInt(fracValue);
den = 1;
}
// return fraction
Fraction fracConv = new Fraction(num, den);
return fracConv;
}
}
uj5u.com熱心網友回復:
substring(int start, int end)包括字符 atstart并排除字符 at end。由于整數不能用 a 決議/,你需要做的fracValue.indexOf("/") 1是只得到分母的數字部分。
uj5u.com熱心網友回復:
首先,您必須了解您的輸入,如果您的輸入是 String a = "6 7",則表示您的字串長度為 3,分配的索引為 0,1 和 2,其中 0 索引為 '6',1 索引為 ' ' and 2 index is '7' 所以,當你使用 a.substring(0, a.indexOf(" ")) 時,這意味著你在說
a.indexOf(" ") = 1 index
您應該得到一個包含 '0' 索引但不包含 1 索引的字串,因為子字串適用于包含的第一個引數和不包含的第二個引數。
在這種情況下 a.substring(a.indexOf(" ") 1, a.length())
您不想在分母中包含“ ”,因此您不應該包含 1 個索引,這就是您在 indexof 中添加 ( 1) 的原因。因此,通過添加 1,您是說僅從 a.indexOf(" ") 1 中選擇值,即 2 索引,并且字串的長度為 3,這意味著您將獲得包含 2 個索引的字串,即 7
如果您的輸入是“6 7”,那么您應該在使用 Integer.parseInt 之前使用“trim()”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/396744.html
標籤:爪哇
上一篇:正則運算式檢查字串是否以特殊字符開頭,如“ ”、“-”、“=”或“@”Java
下一篇:需要列印一個字串陣列
