使用方法和对象,但没有数组

时间:2022-06-06 16:22:48

I was here earlier before, and got pointed in the right direction. But I couldn't get the offered solution to work. It now mostly works, but i'm stuck on one part, and I'm not sure how to proceed. If anyone can point me to the right direction, i'll be grateful. I just need a hint, or pointers to something I'm not seeing.
I'm trying to add a random number of days, and determine what that adds up to. I know I have to have a counter that flips over after 7, but I've tried everything, and keep getting errors

我之前在这里,并指出了正确的方向。但我无法获得提供的解决方案。它现在大部分都有效,但是我被困在一个部分,我不知道该怎么办。如果有人能指出我正确的方向,我将不胜感激。我只需要一个提示,或指向我没有看到的东西。我正在尝试添加随机天数,并确定增加的内容。我知道我必须有一个在7点后翻转的计数器,但我已经尝试了一切,并且不断出错

import java.util.*;
public class pooped
{
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) 
    {
        int day;
        System.out.println(" Days of the week are numbered 1-7"+
                    "From Sunday to Saturday, enter a number now");
        day = console.nextInt();

        System.out.println(isWeek(day));
        printday(day);
    }

    public static boolean isWeek(int day)
    {
        return day >= 0 && day <= 7;
    }

    static Scanner console = new Scanner(System.in);

    public static void addDay()
    {
        int date;
        System.out.println("Enter how many days you want to go forward.");
        date = console.nextInt();

        if (int date > 0)
        {
            int day = date + 1;
        }
    }

    public static void printday(int day) 
    {
        switch (day) {

        case 1:
            System.out.println("Sunday");
        break;

        case 2:
            System.out.println("Monday");
        break;

        case 3:
            System.out.println("Tuesday");
        break;

        case 4:
            System.out.println("Wednesday");
        break;

        case 5:
            System.out.println("Thursday");
        break;

        case 6:
            System.out.println("Friday");
        break;

        case 7:
            System.out.println("Saturday");

        default:
        break;
        }
    }
}

2 个解决方案

#1


If you need

如果你需要

a counter that flips over after 7

一个柜台在7点后翻转

consider using day = day % 7; in your main method, which gives you the modulo operation.

考虑使用day = day%7;在main方法中,它为您提供模运算。

#2


When you say "I have to have a counter that flips over after 7", do you mean that 7 should roll back to 0, 8 to 1, etc? If that's the case, you need to look at the modulus operator (%)

当你说“我必须有一个在7之后翻转的计数器”时,你的意思是7应该回滚到0,8比1等等吗?如果是这种情况,您需要查看模数运算符(%)

You inWeek function looks incorrect. If your day is 0 based, it should be:

你inWeek功能看起来不正确。如果您的日期为0,则应为:

public static boolean isWeek(int day)
{
    return day >= 0 && day < 7;
}

Otherwise you will have 8 days.

否则你将有8天。

#1


If you need

如果你需要

a counter that flips over after 7

一个柜台在7点后翻转

consider using day = day % 7; in your main method, which gives you the modulo operation.

考虑使用day = day%7;在main方法中,它为您提供模运算。

#2


When you say "I have to have a counter that flips over after 7", do you mean that 7 should roll back to 0, 8 to 1, etc? If that's the case, you need to look at the modulus operator (%)

当你说“我必须有一个在7之后翻转的计数器”时,你的意思是7应该回滚到0,8比1等等吗?如果是这种情况,您需要查看模数运算符(%)

You inWeek function looks incorrect. If your day is 0 based, it should be:

你inWeek功能看起来不正确。如果您的日期为0,则应为:

public static boolean isWeek(int day)
{
    return day >= 0 && day < 7;
}

Otherwise you will have 8 days.

否则你将有8天。