Java赋值(更改if语句中的字符串值)

Java赋值(更改if语句中的字符串值),java,string,if-statement,Java,String,If Statement,这是助理。我相信我只能使用If和else语句 以下算法生成季节(春季、夏季、秋季或秋季) (冬天)一天中给定的一个月 IF month is 1,2, or 3, season = "Winter Else if month is 4, 5, or 6, season = "Spring" Else if month is 7,8, 9, season = "Summer" Else if month is 10,11, or 12, season = "Fall" If month is di

这是助理。我相信我只能使用If和else语句

以下算法生成季节(春季、夏季、秋季或秋季) (冬天)一天中给定的一个月

IF month is 1,2, or 3, season = "Winter
Else if month is 4, 5, or 6, season = "Spring"
Else if month is 7,8, 9, season = "Summer"
Else if month is 10,11, or 12, season = "Fall"
If month is divisible by 3 and day >= 21
     If season is "Winter", season = "Spring"
     Else if season is "Spring", season = "Summer"
     Else if season is "Summer", season = "Fall"
     Else season = "Winter"
编写一个程序,提示用户一个月和一天,然后 打印由此算法确定的季节

这是我到目前为止得到的。如果一个月可以被3整除,而一天超过20,我就无法得到最后一部分来改变季节

import java.util.Scanner;

public class Seasons {

    public static void main (String[] args){

        Scanner in = new Scanner(System.in);
        int month;
        int day;
        String season = null;

        System.out.print("Please enter month and day: ");
        month = in.nextInt();
        day = in.nextInt();

        if(1 <= month && month <=3){
            season = "Winter";
            System.out.println(season);
        }

        else if (4 <= month && month <=6){
            season = "Spring";
            System.out.println(season);
        }

        else if (7 <=month && month <=9){
            season = "Summer";
            System.out.println(season);
        }

        else if (10 <= month && month <= 12){
            season = "Fall";
            System.out.println(season);
        }


        if ( ((month % 3 == 0) && (21 <= day)){
            if (season.equals("Winter")){

            }

            else if (season.equals("Spring")){
                season = ("Summer");
            }

            else if (season.equals("Summer")){
                season = "Fall";
            }

            else {
                season = "Winter";
            }


        }
    }
}
import java.util.Scanner;
公共课季节{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
整月;
国际日;
字符串季节=null;
系统输出打印(“请输入月份和日期:”);
月份=in.nextInt();
day=in.nextInt();
如果(1使用模数运算符(%)在除法后获得提醒,如下所示:

if ((month % 3) == 0 && day >= 21)
{
  if (season.equals("Winter")) 
  { 
    season = "Summer";
  }
  // ... etc ...
}
public class Seasons {

  public static void main (String[] args){

    Scanner in = new Scanner(System.in);
    int month;
    int day;
    String season = null;

    System.out.print("Please enter month and day: ");
    month = in.nextInt();
    day = in.nextInt();
    String[] seasons = {"Winter", "Spring", "Summer", "Fall"};
    if (day >= 21 && month % 3 == 0) {
            season = seasons[((month + 2) / 3) % 4];
    } else {
            season = seasons[((month-1)/3)];
    }
    System.out.println("Season: " + season);
  }
}

我认为这是从if块中的逻辑测试中得出的,使用括号作为一个单元进行测试:

if ( ((month % 3) == 0) && (day >= 21) )
编辑:

您需要做的就是将变量重新分配给新的季节

if ( ((month % 3) == 0) && (day >= 21) ) {
    season = "Winter";
} 
继续在
季节中打印

只要所有条件的计算结果均为
true
。则变量将更改为
winter
。但如果它为
false
,则不会更改

您可以简单地尝试:

if(true) {
   season = "Winter";
}

System.out.println(season);

您应该看到
冬季

根据您的情况,类似这样的方法应该有效:

private String getSesson(int month, int day) {
    String season = "";
    switch (month) {
        case 1:
        case 2:
        case 3:
            season = "Winter";
            if(month % 3 == 0 && day >=21)
                season = "Spring";
            break;
        case 4:
        case 5:
        case 6:
            season = "Spring";
            if(month % 3 == 0 && day >=21)
                season = "Summer";
            break;
        case 7:
        case 8:
        case 9:
            season = "Summer";
            if(month % 3 == 0 && day >=21)
                season = "Fall";
            break;
        case 10:
        case 11:
        case 12:
            season = "Fall";
            if(month % 3 == 0 && day >=21)
                season = "Winter";
            break;
        default:
            season = "Not a valid month";
            break;  
    }
    return season;
}
测试:

以上系统打印:

Winter
Winter
Winter
Spring
Spring
Spring
Spring
Summer
Summer
Summer
Summer
Fall
Fall
Fall
Fall
Winter

If..else版本:

    Scanner in = new Scanner(System.in);
    int month, day; String season = null;
    System.out.print("Please enter month and day: ");
    month = in.nextInt(); day = in.nextInt();
    if(1 <= month && month <= 3){
        season = "Winter";
        if((month == 3) && (21 <= day))
            season = "Spring";
    } else if (4 <= month && month <=6){
        season = "Spring";
        if((month == 6) && (21 <= day))
            season = "Summer";
    } else if (7 <=month && month <=9){
        season = "Summer";
        if((month == 9) && (21 <= day))
            season = "Fall";
    } else if (10 <= month && month <= 12){
        season = "Fall";
        if((month == 12) && (21 <= day))
            season = "Winter";
    }
    System.out.println(season);
Scanner-in=新的扫描仪(System.in);
整数月,日;字符串季节=null;
系统输出打印(“请输入月份和日期:”);
月=in.nextInt();日=in.nextInt();

如果(1您的原始算法看起来有误。。 如果在第一部分中确定是冬季(第1、2或3个月),第3个月,白天大于第20个月,则是春季,而不是夏季

下面是它应该说的:

if (month % 3 == 0 && 21 <= day){
    if (season.equals("Winter")){
        season = "Spring"
    }

    else if (season.equals("Spring")){
        season = "Summer";
    }

    else if (season.equals("Summer")){
        season = "Fall";
    }

    else {
        season = "Winter";
    }
}

为了您自己的利益,您可以使用

String season= "Winter,Spring,Summer,Fall".split(",")[(month + day/21 - 1)/3 &3];


代码中的实际问题(通常是有效的)是在更改季节值之前打印季节值。这就是为什么无法看到算法的第二部分何时起作用

删除每个if语句上的
System.out
s,并在方法末尾将其更改为唯一一个。同时修复上一个if/else,因为冬季的if/else为空。 最终代码:

public static void main (String[] args){

    Scanner in = new Scanner(System.in);
    int month;
    int day;
    String season = null;

    System.out.print("Please enter month and day: ");
    month = in.nextInt();
    day = in.nextInt();

    if(1 <= month && month <=3){
        season = "Winter";
    }

    else if (4 <= month && month <=6){
        season = "Spring";
    }

    else if (7 <=month && month <=9){
        season = "Summer";
    }

    else if (10 <= month && month <= 12){
        season = "Fall";
    }


    if ( ((month % 3 == 0) && (21 <= day)){
        if (season.equals("Winter")){
            season="Spring";
        }

        else if (season.equals("Spring")){
            season = "Summer";
        }

        else if (season.equals("Summer")){
            season = "Fall";
        }

        else {
            season = "Winter";
        }


    }

    System.out.println(season);
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
整月;
国际日;
字符串季节=null;
系统输出打印(“请输入月份和日期:”);
月份=in.nextInt();
day=in.nextInt();
if(1
import javax.swing.JOptionPane;
公共类季节检测器{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
String month1=JOptionPane.showInputDialog(“请输入月份号”);
String day1=JOptionPane.showInputDialog(“请输入日期”);
int month=Integer.parseInt(month1);
int day=Integer.parseInt(第1天);

如果((月=3)和(&(日>=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day=21&&day),那么你的问题是什么呢!不清楚!只要把
是多余的。它就在那里。我只是在这些条件满足时很难改变季节。我想我不能用这个开关assigment@Bert:如果
month
是整数,为什么不呢?我的代码中是否有任何内容可以更改以解决问题?快速查看您的代码显示:您忘记分配
季节
变量a值位于
如果(季节等于(“冬季”){
条件下。然后删除
中的
季节=(“夏季”)
。试试@Matt的代码。它似乎是正确的。@Bert:看我的编辑,不要让别人为钱做作业。如果你能用我的原始代码解决这个问题,请付10美元。看看我的第一个解决方案,那是你的原始代码(错误的部分)…用你所拥有的东西替换它,然后它就可以工作了。$10 pay pal,如果你能用我的原始代码解决这个问题。@Bert:这里的事情不是这样的:没有钱,而是投票。我很乐意接受捐赠给你选择的慈善机构的提议。在这项事业中,你可以在每月和每天读完这句话后,用这句话替换所有的东西。任何人都是对你的工作进行评分可能会期望你能够解释你的答案。天哪,这太简单了,我觉得自己很笨。一个描述很有用。
for (int month = 1; month <= 12; month++)
    for (int day = 20; day <= 21; day++) {
        String season = "Winter,Spring,Summer,Fall".split(",")[(month + day / 21 - 1) / 3 & 3];
        System.out.println(day + "/" + month + " => " + season);
    }
20/1 => Winter
21/1 => Winter
20/2 => Winter
21/2 => Winter
20/3 => Winter
21/3 => Spring
20/4 => Spring
21/4 => Spring
20/5 => Spring
21/5 => Spring
20/6 => Spring
21/6 => Summer
20/7 => Summer
21/7 => Summer
20/8 => Summer
21/8 => Summer
20/9 => Summer
21/9 => Fall
20/10 => Fall
21/10 => Fall
20/11 => Fall
21/11 => Fall
20/12 => Fall
21/12 => Winter
public static void main (String[] args){

    Scanner in = new Scanner(System.in);
    int month;
    int day;
    String season = null;

    System.out.print("Please enter month and day: ");
    month = in.nextInt();
    day = in.nextInt();

    if(1 <= month && month <=3){
        season = "Winter";
    }

    else if (4 <= month && month <=6){
        season = "Spring";
    }

    else if (7 <=month && month <=9){
        season = "Summer";
    }

    else if (10 <= month && month <= 12){
        season = "Fall";
    }


    if ( ((month % 3 == 0) && (21 <= day)){
        if (season.equals("Winter")){
            season="Spring";
        }

        else if (season.equals("Spring")){
            season = "Summer";
        }

        else if (season.equals("Summer")){
            season = "Fall";
        }

        else {
            season = "Winter";
        }


    }

    System.out.println(season);
}
import javax.swing.JOptionPane;

public class SeasonDetictor {

    public static void main(String[] args) {
         // TODO Auto-generated method stub
         String month1 = JOptionPane.showInputDialog("please enter number of the month");
         String day1 = JOptionPane.showInputDialog("please enter number of the day");
         int month = Integer.parseInt(month1);
         int day = Integer.parseInt(day1);

         if ((month == 3)&&(day >= 21 && day <= 31) ) {    
             JOptionPane.showMessageDialog(null, "spring"); 
         }

         else if ((month == 4)||(month == 5)) {
             JOptionPane.showMessageDialog(null, "spring"); 
         }

         else if  ((month == 6) && (day < 21)) {
             JOptionPane.showMessageDialog(null, "spring"); 
         }

         if ((month == 6)&&(day >= 21 && day <= 31) ) {    
             JOptionPane.showMessageDialog(null, "summer"); 
         }

         else if ((month == 7)||(month == 8)) {
             JOptionPane.showMessageDialog(null, "summer"); 
         }

         else if  ((month == 9) && (day < 21)) {
             JOptionPane.showMessageDialog(null, "summer"); 
         }

         if ((month == 9)&&(day >= 21 && day <= 31) ) {    
             JOptionPane.showMessageDialog(null, "fall"); 
         }

         else if ((month == 10)||(month == 11)) {
             JOptionPane.showMessageDialog(null, "fall"); 
         }

         else if  ((month == 12) && (day < 21)) {
             JOptionPane.showMessageDialog(null, "fall"); 
         }


         if ((month == 12)&&(day >= 21 && day <= 31) ) {    
             JOptionPane.showMessageDialog(null, "winter"); 
         }

         else if ((month == 1)||(month == 2)) {
             JOptionPane.showMessageDialog(null, "winter"); 
         }

         else if  ((month == 3) && (day < 21)) {
             JOptionPane.showMessageDialog(null, "winter"); 
         }

    }
}