java求讲解
publicclassA4Q2{publicA4Q2(){}publicvoidevaluateExpressionsOnePerLine(){Scanners=newS...
public class A4Q2
{
public A4Q2(){
}
public void evaluateExpressionsOnePerLine(){
Scanner s = new Scanner(System.in);
System.out.println("enter an expression and I'll display the result, or press end-of-file to exit");
while (s.hasNext()){
System.out.println("result = " + evaluateExpression(s.nextLine()) );
System.out.println("enter an expression and I'll display its result, or press end-of-file to exit");
}
System.out.println("Exiting");
}
private int evaluateExpression(String line){
line = replaceOperators(line);
return result(line);
}
private int result(String line){
// set up scanner and process the line
Scanner sl = new Scanner(line);
int result = sl.nextInt(); // first operand
// for each operator and operand
// apply the operator to its operands
while (sl.hasNext()){
String opr = sl.next();
int operand = sl.nextInt();
switch(opr){
case "+": result = result + operand;
break;
case "-": result = result - operand;
break;
case "*": result = result * operand;
break;
case "/": result = result / operand;
break;
case "%": result = result % operand;
break;
}
}
return result;
}
private String replaceOperators(String line){
String lineWithSPace = "";
for (int i=0; i<line.length(); i++){
char c = line.charAt(i);
switch (c) {
case '+':
lineWithSPace += " + ";
break;
case '*':
lineWithSPace += " * ";
break;
case '%':
lineWithSPace += " % ";
break;
case '-':
lineWithSPace += " - ";
break;
case '/' :
lineWithSPace += " / ";
break;
default:
lineWithSPace += c;
}
}
return lineWithSPace;
}
}
希望能有个大佬能够详细的讲解一下上面这个程序。我对代码分块感到无力。 展开
{
public A4Q2(){
}
public void evaluateExpressionsOnePerLine(){
Scanner s = new Scanner(System.in);
System.out.println("enter an expression and I'll display the result, or press end-of-file to exit");
while (s.hasNext()){
System.out.println("result = " + evaluateExpression(s.nextLine()) );
System.out.println("enter an expression and I'll display its result, or press end-of-file to exit");
}
System.out.println("Exiting");
}
private int evaluateExpression(String line){
line = replaceOperators(line);
return result(line);
}
private int result(String line){
// set up scanner and process the line
Scanner sl = new Scanner(line);
int result = sl.nextInt(); // first operand
// for each operator and operand
// apply the operator to its operands
while (sl.hasNext()){
String opr = sl.next();
int operand = sl.nextInt();
switch(opr){
case "+": result = result + operand;
break;
case "-": result = result - operand;
break;
case "*": result = result * operand;
break;
case "/": result = result / operand;
break;
case "%": result = result % operand;
break;
}
}
return result;
}
private String replaceOperators(String line){
String lineWithSPace = "";
for (int i=0; i<line.length(); i++){
char c = line.charAt(i);
switch (c) {
case '+':
lineWithSPace += " + ";
break;
case '*':
lineWithSPace += " * ";
break;
case '%':
lineWithSPace += " % ";
break;
case '-':
lineWithSPace += " - ";
break;
case '/' :
lineWithSPace += " / ";
break;
default:
lineWithSPace += c;
}
}
return lineWithSPace;
}
}
希望能有个大佬能够详细的讲解一下上面这个程序。我对代码分块感到无力。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询