我想为我的计算器做一个清晰的按钮,这样当我在任何时候键入 C 时,它都会从计算器图像重新启动



从计算器图像及以下开始,我希望它清除计算器并从图像重新启动计算器。我该怎么做? 基本上计算器可以做简单的加法、乘法、除法和减法,以及平方根和一些指数。

import java.util.Scanner;
import static java.lang.Math.sqrt;
public class Calculator
{
public static void main(String[] args)
{
String message = "Hello, this calculator can solve any addition, 
division, nnmutliplication, and subtraction problem. And can solve a few 
equations nnPlease type the problem you want to solve then press 
enternnnnnnn";
slowPrint(message, 30);

System.out.println("|━━━━━━━━━━|");
System.out.println("|                 |");
System.out.println("|━━━━━━━━━━|");
System.out.println("|[CE] [C] [+] [-] | ");
System.out.println("|[1] [2] [3] [^]  |");
System.out.println("|[4] [5] [6] [/]  |");
System.out.println("|[7] [8] [9] [*]  |");
System.out.println("|[0] [.] [=] [√]  |");
System.out.println("|                 |");
System.out.println("|━━━━━━━━━━|nn");
Scanner scan = new Scanner (System.in);
Scanner userInput = new Scanner (System.in);
System.out.println("Please type the operation you would like to use exactly how it looks like on the calculator. nn(for sqrt the second number you put is what is being rooted and type sqrt)");
String opchoice = userInput.nextLine();
System.out.println("Please type the first number you would like to use.nn");
String a;
//converts a string to an integer
a = scan.nextLine();
String number = a;
double result = Double.parseDouble(number);

System.out.println("Please type the second number you would like to use.nn");
String b;
b = scan.nextLine();
String secondnumber = b;
double secondresult = Double.parseDouble(secondnumber);

double addsolution;
double subsolution;
double mulsolution;
double divsolution;
double sqrtsolution;
double expsolution;
switch (opchoice){
case "+" :
addsolution = result + secondresult;
System.out.println(""+result+" plus "+secondresult+" equals "+addsolution+" ");
break;
case "-" :
subsolution = result - secondresult;
System.out.println(" "+result+" minus "+secondresult+" equals "+subsolution+" ");
break;
case "*" :
mulsolution = result * secondresult;
System.out.println(" "+result+" times "+secondresult+" equals "+mulsolution+" ");
break;
case "/" :
divsolution = result / secondresult;
System.out.println(" "+result+" divided by "+secondresult+" equals "+divsolution+" ");
break;
case "sqrt" :
sqrtsolution = Math.sqrt(secondresult);
System.out.println(" The square root of "+secondresult+" is  "+sqrtsolution+" ");
break;
case "^" :
expsolution = Math.pow(result, secondresult);
System.out.println(" "+result+" to the power of "+secondresult+" equals "+expsolution+" ");
break;




}

}
/**
* Function to print each character in a string with a delay (a "typewriter" effect)
* @param message The string to print
* @param millisPerChar Milliseconds to take to print each character
*/
public static void slowPrint(String message, long millisPerChar)
{
for (int i = 0; i < message.length(); i++)
{
System.out.print(message.charAt(i));
try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}

}

我认为这就是你想要的:

String[] img = new String[]{
"|━━━━━━━━━━|",
"|                 |",
"|━━━━━━━━━━|",
"|[CE] [C] [+] [-] | ",
"|[1] [2] [3] [^]  |",
"|[4] [5] [6] [/]  |",
"|[7] [8] [9] [*]  |",
"|[0] [.] [=] [√]  |",
"|                 |",
"|━━━━━━━━━━|nn"
};
//all you need to do is use these three lines of code when you want to 
//reshow it
for (int i = 0; i < img.length; i++) {
System.out.println(img[i]);
}

将字符串图像存储在字符串数组中,然后在需要时打印出不同的行。您也可以将其存储在一个字符串中,行之间为 。主要部分是存储它,以便可以重复使用。

相关内容

最新更新