我应该创建一个数学家教。我能够创建一个工作良好的指示。但是,我应该收集尝试的次数和正确的问题数量,并将它们发送到第二类。然后计算尝试的次数使用函数和计算正确的问题数量使用函数。图一个分数,并显示该信息。
public class Math_Tutor
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
int choice;
Scanner input = new Scanner(System.in);
System.out.print("Please enter your name: ");
String name = input.nextLine();
Student student = new Student();
student.setName(name);
do
{
System.out.println("*** Math Tutor Menu ***");
System.out.println(
"Choose the type of problem:\n" +
"1. Addition\n" +
"2. Subtraction\n" +
"3. Multiplication\n" +
"4. Exit"
);
System.out.print("Enter your choice: ");
choice = input.nextInt();
switch (choice)
{
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiplication();
break;
}
} while (choice!=4);
student.results();
}
public static void addition()
{
int keepGoing;
do
{
int firstNum = (int)(Math.random() * 10);
int secondNum = (int)(Math.random() * 10);
int add = firstNum + secondNum;
System.out.println(firstNum + " + " + secondNum + " ?");
Scanner input = new Scanner(System.in);
int addInput = input.nextInt();
if (addInput == add)
System.out.println("Correct!");
else
System.out.println("Your answer is wrong. The correct answer is "+ add);
System.out.println("Would you like to keep going?\n"
+ "1. Yes\n"
+ "2. No");
System.out.print("Enter your choice: ");
keepGoing=input.nextInt();
} while (keepGoing != 2);
}
public static void subtraction()
{
int keepGoing;
do
{
int firstNum = (int)(Math.random() * 10);
int secondNum = (int)(Math.random() * 10);
if (firstNum < secondNum)
{
int temp = firstNum;
firstNum = secondNum;
secondNum = temp;
}
int subtract = firstNum - secondNum;
System.out.println(firstNum + " - " + secondNum + " ?");
Scanner input = new Scanner(System.in);
int subInput = input.nextInt();
if (subInput == subtract)
System.out.println("Correct!");
else
System.out.println("Your answer is wrong. The correct answer is "+ subtract);
System.out.println("Would you like to keep going?\n"
+ "1. Yes\n"
+ "2. No");
System.out.print("Enter your choice: ");
keepGoing=input.nextInt();
} while(keepGoing != 2);
}
public static void multiplication()
{
int keepGoing;
do
{
int firstNum = (int)(Math.random() * 10);
int secondNum = (int)(Math.random() * 10);
int multiply = firstNum * secondNum;
System.out.println(firstNum + " * " + secondNum + " ?");
Scanner input = new Scanner(System.in);
int multInput = input.nextInt();
if (multInput == multiply)
System.out.println("Correct!");
else
System.out.println("Your answer is wrong. The correct answer is "+ multiply);
System.out.println("Would you like to keep going?\n"
+ "1. Yes\n"
+ "2. No");
System.out.print("Enter your choice: ");
keepGoing=input.nextInt();
} while(keepGoing != 2);
}
}
这是第二节课
public class Student
{
private String student;
private int attempted;
private int correct;
private int score;
public String setName(String name)
{
student = name;
return student;
}
public int attempted(int attempt)
{
attempted = attempt;
return attempted;
}
public int correct(int right)
{
correct = right;
return correct;
}
public void results()
{
System.out.print("*** Results for " + student + " ***");
// System.out.print("Total number of problems attempted: " + attempted);
// System.out.print("Total number of problems correct: " + correct);
// score = (correct / attempted) * 100;
// System.out.print(student + " scored : " + score);
// if (score >= 90)
// System.out.print("Great work " + student);
// else if (score >= 80 && score < 90)
// System.out.print("Good work " + student);
// else if (score >= 70 && score < 80)
// System.out.print("Not bad " + student);
// else
// System.out.print("Keep trying");
}
}
我必须计算在加、减、乘函数中的尝试次数和正确的问题数,并将这些数字发送给学生类,我尝试过在几个地方创建变量,以便将计数发送给主类,这样我就可以将它们发送过来,但没有任何效果。
2条答案
按热度按时间eoxn13cs1#
您需要确保在
main
方法中创建的student
对象可以从addition()
、substraction()
等方法访问。要实现这一点,您可以将
student
对象作为参数传递给这些方法,或者将student
声明为类MathTutor
上的静态变量,如下所示:接下来,为了计算尝试次数,您需要从
addition()
、substraction()
等中调用您学生的attempted
和correct
方法。我将这些方法更改为如下所示,以便它们增加student
对象的内部计数器,例如:最后,
results()
方法可以打印出学生的成绩,就像在注解掉的代码中所做的那样。jm81lzqq2#
由于这是一个练习,我将避免解决它,但我会提供你所需要的指导,以确保你将能够解决它。
让我们考虑这个类:
现在,让我们考虑另一类
我们在
doSomething
中使用了getter,以便从Foo
class
的foo
示例中获取一些值。您也可以定义一个public
数据成员,但我不推荐这样做。回到您的示例:有一个名为
setName
的public
setter是非常好的,但是您还需要区分数据阅读和数据写入,以便管理尝试的数据和正确的数据。目前有两个方法的setter和getter特性都很混乱。您需要有一个getAttempted
方法才能读取它(这将从类外部使用以获取尝试的值)和一个setAttempted
值来更改此值。然后,您将能够正确地设置和获取这些值。