多种方法的参数帮助

时间:2017-03-16 20:34:58

标签: java

所以我有一个分配来完成以下两个参数(总共有四个,但前两个参数不容易)

参数1:coinToss

这种方法模拟投掷硬币并打印出来" Head"或" Tail" 例如,当调用此方法时,它将随机打印出" Head"或" Tail"

参数2:dayOfWeek

给定一个数字(1-7),此方法返回一周中适当的日期。 例如,给定1此方法返回"星期日"。

我实际上能够使用我的OWN方法完成这两个参数并且工作得很好。但他最近上传了他希望我们这样做的方式,现在需要帮助!

他希望我们这样做:

参数3:

    m.coinToss();

参数4:

    System.out.print("Type any number (1-7): ");
    int day = in.nextInt();
    String dayOfWeek = m.dayOfWeek(day);
    System.out.printf("%s is the %d day of the week.\n", dayOfWeek, day);

我是怎么做到的:

public void coinToss (int r) {
    boolean headOrTail = (r % 2 == 0);
    if (headOrTail) {
        System.out.println("Heads");
    } else {
        System.out.println("Tails.");
    }
}

public void dayOfWeek (int whichDay) {
    Scanner in = new Scanner (System.in);
    Homework2 m = new Homework2();
    if (whichDay == 1) {
        System.out.println("Sunday.");
    }
    if (whichDay == 2) {
        System.out.println("Monday.");
    }
    if (whichDay == 3) {
        System.out.println("Tuesday.");
    }
    if (whichDay == 4) {
        System.out.println("Wednesday.");
    }
    if (whichDay == 5) {
        System.out.println("Thursday.");
    }
    if (whichDay == 6) {
        System.out.println("Friday.");
    }
    if (whichDay == 7) {
        System.out.println("Saturday.");
    }
    if (whichDay > 7) {
        int day;
        System.out.print("Please enter a number from 1 and 7. ");
        day = in.nextInt();
        m.dayOfWeek(day);       
    }
}

public static void main(String[] args) {
    int headOrTail = random.nextInt(100) + 1;
    m.coinToss(headOrTail);
    System.out.println();
    System.out.print("Type any number (1-7): ");
    int day = in.nextInt();
    m.dayOfWeek(day);
}

3 个答案:

答案 0 :(得分:0)

coinToss的实现大部分都很好,你所缺少的基本上是一种生成随机数的方法。在Java中,你有java.util.Random类就可以做到:

Random random = new Random();
int randomInteger = random.nextInt();

现在你有了你的随机整数,我会让你把它插入你的硬币投掷:))

Random的参考:http://docs.oracle.com/javase/8/docs/api/java/util/Random.html

编辑:奖励积分:由于您只有两个可能的值,即头部和尾部,您可以使用nextBoolean()方法并简化您的抛硬币条件。祝你好运!

答案 1 :(得分:0)

参数3:

m.coinToss();

由于cointoss方法所需的实现不需要参数,您应该做的是生成内部 cointoss方法的随机数,然后检查该数字是否为偶数或奇数。如果均匀,请打印Head,否则请打印Tail

移动以下行

int headOrTail = random.nextInt(100) + 1;

cointoss方法内,然后检查headOrTail奇数还是甚至

示例:

public void coinToss () {

    int headOrTail = random.nextInt(100) + 1;

    if ((headOrTail % 2) == 0) {
       System.out.println("Heads");
    }
    else {
       System.out.println("Tails.");
    }
}

参数4:

对于dayOfWeek方法,您需要从该方法返回星期String,然后将其存储在

String dayOfWeek = m.dayOfWeek(day);

dayOfWeek变量。

然后您可以同时打印dayOfWeekday

您必须将dayOfWeek方法的返回类型从void更改为String。此外,用于存储dayOfWeek方法的返回值的变量的名称和方法的名称相同,这可能会产生歧义。考虑为它们使用不同的名称。

示例:

public String dayOfWeek (int whichDay) {

   if (whichDay == 1) {
       return "Sunday";
   }
   //other if statements
}

答案 2 :(得分:0)

你可以打印dayOfWeek和day ..所以你试试这个。

您需要将dayOfWeek方法的返回类型从void更改为String。

import java.util.Random;
import java.util.Scanner;

public class WeekDay {
    static WeekDay m = new WeekDay();
    static Scanner in = new Scanner (System.in);
    static Random rand = new Random();
    String day;
    public void coinToss () {
        int headOrTail = rand.nextInt(100) + 1;
        boolean check = (headOrTail % 2 == 0);
        if (check) {
            System.out.println("Heads");
        }else {
            System.out.println("Tails.");
        }

        }

        public String dayOfWeek (int whichDay) {


        if (whichDay == 1) {
            day = "Sunday.";
        }
        if (whichDay == 2){
            day = "Monday.";
        }
        if (whichDay == 3){
            day = "Tuesday.";
        }
        if (whichDay == 4){
            day = "Wednesday.";
        }
        if (whichDay == 5){
            day = "Thursday.";
        }
        if (whichDay == 6){
            day = "Friday.";
        }
        if (whichDay == 7){
            day = "Saturday.";

        }
        if (whichDay > 7) {
            int dayCount;
            System.out.print("Please enter a number from 1 and 7. ");
            dayCount = in.nextInt();
            m.dayOfWeek(dayCount);
        }
        return day;
        }

        public static void main(String[]args){
            m.coinToss();
            System.out.println("\n");
            System.out.print("Type any number (1-7): ");
            int dayCount = in.nextInt();
            m.dayOfWeek(dayCount);

            String dayOfWeek = m.dayOfWeek(dayCount);
            System.out.printf("%s is the %d day of the week.\n", dayOfWeek, dayCount);
        }

}