你能检查一下输入是否与变量名相同吗?

huangapple go评论65阅读模式
英文:

Can you check to see if an input is the same as a variable name?

问题

我正在使用Java Main进行编码。
我正在编写一个程序,用于计算游戏中每日奖励所获得的资源。我已经为每个月声明了一个整数变量,它保存了该月的天数值。每个变量都包括它自己以及所有之前的月份的天数,如下所示:

     int month;
        int january = 31;
        int february = 28; 59
        int march = 31; 90
        int april = 30; 120
        int may = 31; 151
        int june = 30; 181
        int july = 31; 212
        int august = 31; 243
        int september = 30; 273
        int october = 31; 304
        int november = 30; 334
        int december = 31; 365

后面跟随的数字是语句中将要使用的内容,我只是没有放进去,这样我就可以将天数作为参考。
无论如何,我想要做的是让用户输入当前月份和他们想要了解其资源数量的月份,分别保存为String变量currentMonth和laterMonth。我想要做的是检查字符串变量(比如,"September" 和 "March")是否与其对应的整数变量同名,如果是,则从整数变量 "march" 中减去整数变量 "september"(273 - 90),以获取之间的天数,从而得到整数变量 "month"。是否可能进行这样的检查?也许我需要设置一个数组?感谢您的帮助!

英文:

I am coding in Java Main.
I am writing a program that will count the resources gained from a daily rewards in a game. I have declared an Int variable for every month that holds the value for the number of days in that month. Each variable includes the number of days for itself, and all previous months, as so:

     int month;
        int january = 31;
        int february = 28; 59
        int march = 31; 90
        int april = 30; 120
        int may = 31; 151
        int june = 30; 181
        int july = 31; 212
        int august = 31; 243
        int september = 30; 273
        int october = 31; 304
        int november = 30; 334
        int december = 31; 365

the number that follows is what will be in the statement, i just have not put them in so i can keep the #of days as reference.
Anyway, what I want to do is have the user input the current month and the month they want to know how many resources they will have, saved as String variables currentMonth and laterMonth respectively. What I want to do is check to see if the String variables (say, September and March) have the same name as their int var counterparts, and if so, subtract int march from int september (273 - 90) to get the number of days in between to get int month. Is it possible for there to be such a check? Maybe I need to set up an array? I appreciate your help!

答案1

得分: 2

你不需要为此维护一个数组。java.time API 为您提供了许多选项,您可以利用这些选项来实现您的需求。下面是一个示例:

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println(Month.FEBRUARY.maxLength());

        // Difference between 31st Mar and 30th Sep
        System.out.println(
            Math.abs(ChronoUnit.DAYS.between(LocalDate.of(2020, Month.SEPTEMBER, Month.SEPTEMBER.maxLength()),
                    LocalDate.of(2020, Month.MARCH, Month.MARCH.maxLength()))));

        // ############## Interactive ##############
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the first month[1-12]: ");
        int first = scanner.nextInt();

        System.out.print("Enter the second month[1-12]: ");
        int second = scanner.nextInt();

        System.out.println(Math.abs(ChronoUnit.DAYS.between(LocalDate.of(2020, first, Month.of(first).maxLength()),
                LocalDate.of(2020, second, Month.of(second).maxLength()))));
    }
}

一个示例运行:

29
183
Enter the first month[1-12]: 3
Enter the second month[1-12]: 9
183
英文:

You do not need to maintain an array for it. The java.time API provides you with a lot of options which you can leverage to achieve what you need. Given below is an example:

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		System.out.println(Month.FEBRUARY.maxLength());

		// Difference between 31st Mar and 30th Sep
		System.out.println(
				Math.abs(ChronoUnit.DAYS.between(LocalDate.of(2020, Month.SEPTEMBER, Month.SEPTEMBER.maxLength()),
						LocalDate.of(2020, Month.MARCH, Month.MARCH.maxLength()))));

		// ############## Interactive ##############
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter the first month[1-12]: ");
		int first = scanner.nextInt();

		System.out.print("Enter the second month[1-12]: ");
		int second = scanner.nextInt();

		System.out.println(Math.abs(ChronoUnit.DAYS.between(LocalDate.of(2020, first, Month.of(first).maxLength()),
				LocalDate.of(2020, second, Month.of(second).maxLength()))));
	}
}

A sample run:

29
183
Enter the first month[1-12]: 3
Enter the second month[1-12]: 9
183

答案2

得分: 1

我认为这个问题可以通过使用 HashMap 或者两个数组来解决。
在我看来,使用两个数组的方法稍微容易一些:

static String[] monthNames = new String[]{
		"一月",
        "二月",
        "三月",
        "四月",
        "五月",
        "六月",
        "七月",
        "八月",
        "九月",
        "十月",
        "十一月",
        "十二月"
	};

public static void main(String[] args) {
	
	int[] numOfDays = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
	int mo2 =	getConsecutiveMonth("九月");
	int mo1 =	getConsecutiveMonth("三月");
	int septemberMarch = sumBetween(numOfDays, mo1, mo2);
	System.out.println(septemberMarch);

}

public static int getConsecutiveMonth(String m){
	String lowercaseM = m.toLowerCase();
	for(int i = 0; i<monthNames.length; i++){
		if(monthNames[i].equals(lowercaseM))
			return i;
	}
	return -1;
}

public static int sumBetween(int[] a, int i, int j){
	int sum = 0;
	for (int idx = i; idx < j; idx++) {
		sum += a[idx];
	}
	return sum;
}
英文:

I think the problem can be solved using either a HashMap or two arrays.
The two arrays approach is a bit easier in my opinion:

static String[] monthNames = new String[]{
		&quot;january&quot;,
        &quot;february&quot;,
        &quot;march&quot;,
        &quot;april&quot;,
        &quot;may&quot;,
        &quot;june&quot;,
        &quot;july&quot;,
        &quot;august&quot;,
        &quot;september&quot;,
        &quot;october&quot;,
        &quot;november&quot;,
        &quot;december&quot;
	};

public static void main(String[] args) {
	

	int[] numOfDays = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
	int mo2 =	getConsecutiveMonth(&quot;september&quot;);
	int mo1 =	getConsecutiveMonth(&quot;march&quot;);
	int septemberMarch = sumBetween(numOfDays, mo1, mo2);
	System.out.println(septemberMarch);

}

public static int getConsecutiveMonth(String m){
	String lowercaseM = m.toLowerCase();
	for(int i = 0; i&lt;monthNames.length; i++){
		if(monthNames[i].equals(lowercaseM))
			return i;
	}
	return -1;
}

public static int sumBetween(int[] a, int i, int j){
	int sum = 0;
	for (int idx = i; idx &lt; j; idx++) {
		sum += a[idx];
	}
	return sum;
}

huangapple
  • 本文由 发表于 2020年8月31日 23:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63673847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定