我正在制作一个可视计算器,这是我现在的代码:
import java.util.*;
public class Visicalc {
public static int i = 0;
public static int j = 0;
public static String[][] values = new String [10][10];
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Type 'help' if you would like to see the commands");
String input = "";
while(input != "quit"){
input = console.nextLine();
if (input.toLowerCase().contains("help"))
{
help();
}
else if(input.contains("print")) {
spreadsheet(input);
}
else if(input.toLowerCase().contains("quit")) //Exits program is user types the word quit
{
System.out.println("Goodbye!"); //Prints Goodbye before the program exits
return;
}
//else if (input != "help" || input != "print" || input != "quit") {
// System.out.println("Invalid command. Try again.");
//}
}
}
public static void help() {
System.out.println("Commands:");
System.out.println("quit This exits the spreadsheet program");
System.out.println("help Prints out all available commands.");
System.out.println("print This prints out the current spreadsheet in its entirety.");
System.out.println("<cell reference> = <value> This is used to place data into a cell. Here are some examples:");
System.out.println("Here are some examples:");
System.out.println("A1 = 6.983");
System.out.println("B2 = Hello there people!");
System.out.println("C5 = 07/14/1955");
System.out.println("<cell reference> : This is used to print the contents of a cell. This must print out exactly what was input to the cell, which is not necessarily what gets printed in the cell when the spreadsheet is printed. ");
System.out.println("clear <cell reference> This is used to clear out a cell and make it empty.");
System.out.println("clear This is used to clear the entire spreadsheet and make all cells empty.");
System.out.println("sorta <cell range> This is used to sort a block of cells in ascending order. All cells in the range must be of the same type in order to be sorted. A cell range is specified as 2 cell references separated by a minus (e.g. sorta A1 B3).");
System.out.println("sortd <cell range> Same as sorta except the cells are sorted in descending order.");
System.out.println();
System.out.println("TYPE 'QUIT' IF YOU WANT TO EXIT THE PROGRAM");
}
public static void spreadsheet(String input) {
values[0][0] = " "; values[1][0] = " "; values[2][0] = " "; values[3][0] = " "; values[4][0] = " ";
values[0][1] = " "; values[1][1] = " "; values[2][1] = " "; values[3][1] = " "; values[4][1] = " ";
values[0][2] = " "; values[1][2] = " "; values[2][2] = " "; values[3][2] = " "; values[4][2] = " ";
values[0][3] = " "; values[1][3] = " "; values[2][3] = " "; values[3][3] = " "; values[4][3] = " ";
values[0][4] = " "; values[1][4] = " "; values[2][4] = " "; values[3][4] = " "; values[4][4] = " ";
values[0][5] = " "; values[1][5] = " "; values[2][5] = " "; values[3][5] = " "; values[4][5] = " ";
values[0][6] = " "; values[1][6] = " "; values[2][6] = " "; values[3][6] = " "; values[4][6] = " ";
values[0][7] = " "; values[1][7] = " "; values[2][7] = " "; values[3][7] = " "; values[4][7] = " ";
values[0][8] = " "; values[1][8] = " "; values[2][8] = " "; values[3][8] = " "; values[4][8] = " ";
values[0][9] = " "; values[1][9] = " "; values[2][9] = " "; values[3][9] = " "; values[4][9] = " ";
values[5][0] = " "; values[6][0] = " "; values[7][0] = " "; values[8][0] = " "; values[9][0] = " ";
values[5][1] = " "; values[6][1] = " "; values[7][1] = " "; values[8][1] = " "; values[9][1] = " ";
values[5][2] = " "; values[6][2] = " "; values[7][2] = " "; values[8][2] = " "; values[9][2] = " ";
values[5][3] = " "; values[6][3] = " "; values[7][3] = " "; values[8][3] = " "; values[9][3] = " ";
values[5][4] = " "; values[6][4] = " "; values[7][4] = " "; values[8][4] = " "; values[9][4] = " ";
values[5][5] = " "; values[6][5] = " "; values[7][5] = " "; values[8][5] = " "; values[9][5] = " ";
values[5][6] = " "; values[6][6] = " "; values[7][6] = " "; values[8][6] = " "; values[9][6] = " ";
values[5][7] = " "; values[6][7] = " "; values[7][7] = " "; values[8][7] = " "; values[9][7] = " ";
values[5][8] = " "; values[6][8] = " "; values[7][8] = " "; values[8][8] = " "; values[9][8] = " ";
values[5][9] = " "; values[6][9] = " "; values[7][9] = " "; values[8][9] = " "; values[9][9] = " ";
if(input.contains("A1")){
values[0][0] = (input.substring(input.indexOf("=") + 1));
}
for (i = 0; i < 10; i++) {
System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
for (j = 0; j < 10; j++) {
System.out.print(values[i][j] + " | ");
}
System.out.println( "" );
}
}
}
正如你在行上看到的那样:
if(input.contains("A1")){
values[0][0] = (input.substring(input.indexOf("=") + 1));
}
应该将电子表格左上角的值更改为输入的值...(例如A1 = Hello)但是一旦放入该代码,我再次打印电子表格,电子表格就是仍然空白,任何帮助表示赞赏。