我目前正在进行自学课程,需要为以下代码编写测试程序,并通过为成员字段分配不同的值来重复步骤。
public class DayTwo {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void displayDay() {
int dayNumber = 1;
if (dayNumber == 1) {
System.out.println("Monday");
} else if (dayNumber == 2) {
System.out.println("Tuesday");
} else if (dayNumber == 3) {
System.out.println("Wednesday");
} else if (dayNumber == 4) {
System.out.println("Thursday");
} else if (dayNumber == 5) {
System.out.println("Friday");
} else if (dayNumber == 6) {
System.out.println("Saturday");
} else if (dayNumber == 7) {
System.out.println("Sunday");
} else {
System.out.println(dayNumber + " entered. This is not a valid date.");
}
}
}
有人可以帮忙吗?
答案 0 :(得分:0)
int dayNumber = 1;
它是硬编码值,并且始终满足跳过其余语句的第一个条件,而不是使用Scanner
或BufferedInputStream
Scanner sc = new Scanner(System.in);
int dayNumber = sc.nextInt();
答案 1 :(得分:0)
首先,所有不运行的类不带主方法。您的代码的另一个错误是它将始终打印“星期一”。 用于制作测试类
public class Test{
public static void main(String[] args){
DayTwo dayTwo = new DayTwo();
dayTwo. displayDay();
}
}
答案 2 :(得分:0)
像这样......
import java.util.*;
public class DayTwo
{
public static void main(String[] args)
{
int number,choice=1;
Scanner sc = new Scanner(System.in);
while(choice!=0)
{
System.out.println("Enter the day number: ");
number=sc.nextInt();
displayDay(number);
System.out.println("Press 0 to exit, 1 to continue:");
choice=sc.nextInt();
}
}
public static void displayDay(int dayNumber)
{
if (dayNumber == 1) {
System.out.println("Monday");
} else if (dayNumber == 2) {
System.out.println("Tuesday");
} else if (dayNumber == 3) {
System.out.println("Wednesday");
} else if (dayNumber == 4) {
System.out.println("Thursday");
} else if (dayNumber == 5) {
System.out.println("Friday");
} else if (dayNumber == 6) {
System.out.println("Saturday");
} else if (dayNumber == 7) {
System.out.println("Sunday");
} else {
System.out.println(dayNumber + " entered. This is not a valid date.");
}
}
}
答案 3 :(得分:0)
如果您想纯粹测试您的代码,请从DayTwo中删除public static void main(String[] args){}
,因为它不需要,并使用以下Test类:
public class TestDay2 {
public static void main(String[] args) {
DayTwo example = new DayTwo();
example.displayDay();
}
}
然而,由于dayNumber是硬编码的,因此总是打印'Monday'。你必须改变它来测试每个if语句,因为我认为你还没有接触过循环。
答案 4 :(得分:0)
要按照说明,只需剪切并粘贴dayNumber
声明,使其成为成员字段(class
声明和main
声明之间)。然后在new DayTwo
中创建main
,您可以将dayNumber
分配给不同的内容并打印出来。
public class DayTwo {
// now a dayNumber belongs to every DayTwo object
int dayNumber = 1;
// main is static and belongs to the DayTwo class
public static void main(String[] args) {
// make a new DayTwo object
DayTwo days = new DayTwo();
// displays Monday
days.displayDay();
days.dayNumber = 4;
// displays Thursday
days.displayDay();
for (int i = 1; i <= 7; i++) {
days.dayNumber = i;
// displays all days
days.displayDay();
}
}
public void displayDay() {
if (dayNumber == 1) {
System.out.println("Monday");
} else if (dayNumber == 2) {
System.out.println("Tuesday");
} else if (dayNumber == 3) {
System.out.println("Wednesday");
} else if (dayNumber == 4) {
System.out.println("Thursday");
} else if (dayNumber == 5) {
System.out.println("Friday");
} else if (dayNumber == 6) {
System.out.println("Saturday");
} else if (dayNumber == 7) {
System.out.println("Sunday");
} else {
System.out.println(dayNumber + " entered. This is not a valid date.");
}
}
}
你不需要比这更复杂的东西。