我的程式應該接受一行整數和字符,將整數和字符分開,將它們存盤在兩個不同的鏈表中,然后根據輸入運行計算。我的計算方法代碼:
public static float ComputeResults(LinkedList<Integer> ints, LinkedList<Character> chars){
//iterate through elements from the int linkedList
result = 0;
for (int counter_int = 0; counter_int < ints.size(); counter_int ){
for (int counter_char = 0; counter_char < chars.size(); counter_char ){
if (chars.get(counter_char).equals(" ") ){
result = result ints.get(counter_int);
}
else if (chars.get(counter_char).equals("-")){
result = result - ints.get(counter_int 1);
}
else if (chars.get(counter_char).equals("*")){
result = result * ints.get(counter_int 1);
}
else if (chars.get(counter_char).equals("/")){
result = result / ints.get(counter_int 1);
}
else {
System.out.println("no if statement is valid");
}
}
}
return result;
}
我明白盡管用戶輸入了什么,但代碼沒有輸入任何 if 陳述句(因為從未找到等于“ ”、“-”、“*”或“/”的字符)?我在 if 條件下做錯了什么?
我的所有其他類的代碼:
UserInterface Class:
import java.util.*;
public class UserInterface {
//create an arraylist to store the numbers
//create another arraylist to store the characters
public static ArrayList<Integer> integers = new ArrayList<Integer>();
public static ArrayList<Character> characters = new ArrayList<Character>();
public static void PrintMenu(){
//ask the user to enter a single line of integers and characters
//ask the user to enter a formula with spaces between the ints and characters
Scanner input = new Scanner (System.in);
System.out.println("Please enter a single line of integers and operators for calculation");
System.out.println("put one space between the integers and characters");
System.out.println("Example of a correctly entered line for calculation: ");
System.out.println("23 4 78 / 31 - 2");
System.out.println("");
//take the input in as a string
String line;
line = input.nextLine();
//parse the line to separate each element of the line
//store them in an arraylist of string
ArrayList<String> elements = new ArrayList();
Scanner scanner = new Scanner(line);
scanner.useDelimiter(" ");
while (scanner.hasNext()){
elements.add(scanner.next());
}
// in the elements arraylist
//if the index of the element is even
//add to list of ints
//if the index of the element is odd
//add to list of chars
for (int i = 0; i<elements.size(); i ){
if (i % 2 == 0){
integers.add(Integer.parseInt(elements.get(i)));
}
else if (i % 2 == 1){
characters.add(elements.get(i).charAt(0));
}
}
}
public static void OutputResults(){
}
public UserInterface(){
}
}
LListCalculator 類的代碼:
import java.util.*;
public class LListCalculator {
static float result;
public static LinkedList LinkedListMaintenance(ArrayList list){
LinkedList linkedList = new LinkedList();
for (int i = 0; i<list.size(); i ){
linkedList.add(list.get(i));
}
return linkedList;
}
public static float ComputeResults(LinkedList<Integer> ints, LinkedList<Character> chars){
//as mentioned before
}
public LListCalculator() {
}
}
主類代碼:
import java.util.*;
public class LListDriver {
public static void main(String[] args)
{
UserInterface.PrintMenu();
LinkedList ints;
LinkedList chars;
ints = LListCalculator.LinkedListMaintenance(UserInterface.integers);
chars = LListCalculator.LinkedListMaintenance(UserInterface.characters);
System.out.println(chars);
float result;
result = LListCalculator.ComputeResults(ints, chars);
System.out.println(result);
}
}
uj5u.com熱心網友回復:
如果您的代碼即將輸入條件 - 請檢查您是否將其與字串或實際字符進行比較:
例如:
@Test
public void test() {
Character theChar = ' ';
if (theChar.equals(" ")) {
System.out.println("Fine");
} else {
System.out.println("Wrong");
}
}
將輸出“錯誤”。
以下將輸出“Fine”:
@Test
public void test() {
Character theChar = ' ';
if (theChar.equals(' ')) {
System.out.println("Fine");
} else {
System.out.println("Wrong");
}
}
uj5u.com熱心網友回復:
equalsequals任何 Java 類中的方法都是 Object 類方法的重寫實作。
這意味著equals方法的簽名是public boolean equals(Object obj)
在上面的實作中,將 Character 實體與 String 實體進行比較chars.get(counter_char).equals(" ")。
這不會導致編譯錯誤,因為 equals 方法的輸入引數型別是 Object(java 中所有其他型別的父類,因此能夠指向任何 java 型別的參考,在本例中為 String 型別),但是相等性檢查由于 LHS 和 RHS 的型別不匹配,將在運行時失敗。
要解決此問題,只需更新等號以將字符與字符進行比較。
chars.get(counter_char).equals(' ')
注意:在 Java 中,雙引號內的值表示一個 sring 常量,而單引號內的值表示一個字符常量。
參考:
- https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#equals-java.lang.Object-
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/421745.html
標籤:
