如果所有日期都没有错,我需要打印一些东西 [英] I need to print something if all of the dates are not wrong

查看:63
本文介绍了如果所有日期都没有错,我需要打印一些东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我尝试运行该程序时都会说这个名为`wm'的局部变量不能在这个范围内声明,因为它会给`wm'赋予不同的含义,它已经在`parent或current'范围内使用了表示其他东西

ii尝试使用布尔但无法做到这一点

我应该更改什么?

这是代码:

every time i try to run the program it says A local variable named `wm' cannot be declared in this scope because it would give a different meaning to `wm', which is already used in a `parent or current' scope to denote something else
ii tried using booleans but couldnt do it
what should i change?
this is the code:

using System;

class MainClass {
  public static void Main (string[] args) {
  int wd = 1;
	int wm = 1;
	int wy = 1;
    Console.WriteLine ("enter digit between 0-4");
    int md = int.Parse(Console.ReadLine());
   //md=menu digit
    if (md==0)
{
    Console.WriteLine("Bye Bye");
}
else if (md==1)
{
	Console.Write("enter month number-");
	int month = int.Parse(Console.ReadLine());
	Console.Write("enter a day-");
	int day = int.Parse(Console.ReadLine());
	Console.Write("enter a year-");
	int year = int.Parse(Console.ReadLine());
	if (month<0 || month>12){
	  Console.WriteLine("wrong month");
	  int wm = 0;
	}
	else if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 && day>31 || day<0){
	  Console.WriteLine("wrong day");
	  int wd = 0;
	}
	else if (month==4 || month==6 || month==9 || month==11){
	  if (day>30 || day<0){
	  Console.WriteLine("wrong day");
	  int wd = 0;
	}
}
if(month==2)
{
  if (year%400==0 || year%4==0)
  {
    if (day>29 || day<0){
      Console.WriteLine("wrong day");
      int wd = 0;
    }
  }
    else {
      if (day<0 || day>28){
      Console.WriteLine("wrong day");
      int wd = 0;
    }
  }
  if (year<0 || year>9999){
    Console.WriteLine("wrong year");
    int wy = 0;
  }
  
}
}
}
}                                         





我尝试了什么:



i试图用布尔值替换数字并在谷歌搜索



What I have tried:

i tried to replace the numbers with booleans and searching on google

推荐答案

当你在变量名之前放入 int 时,它是一个声明,你可以在一个过程中只声明一次变量。 />
解决方案:在每个 wd 前面删除 int ,但第一个。你可能必须对其他变量做同样的事情。

-----

学会正确地缩进你的代码,它显示它的结构,它有助于阅读和理解。它还有助于发现结构错误。

When you put int before a variable name, it is a declaration, and you can declare a variable only 1 time in a procedure.
Solution: remove the int in front of every wd but the first one. you may have to do the same for other variables.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
using System;

class MainClass {
  public static void Main (string[] args) {
    int wd = 1;
    int wm = 1;
    int wy = 1;
    Console.WriteLine ("enter digit between 0-4");
    int md = int.Parse(Console.ReadLine());
    //md=menu digit
    if (md==0)
    {
      Console.WriteLine("Bye Bye");
    }
    else if (md==1)
    {
      Console.Write("enter month number-");
      int month = int.Parse(Console.ReadLine());
      Console.Write("enter a day-");
      int day = int.Parse(Console.ReadLine());
      Console.Write("enter a year-");
      int year = int.Parse(Console.ReadLine());
      if (month<0 || month>12){
        Console.WriteLine("wrong month");
        int wm = 0;
      }
      else if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 && day>31 || day<0){
        Console.WriteLine("wrong day");
        int wd = 0;
      }
      else if (month==4 || month==6 || month==9 || month==11){
        if (day>30 || day<0){
          Console.WriteLine("wrong day");
          int wd = 0;
        }
      }
      if(month==2)
      {
        if (year%400==0 || year%4==0)
        {
          if (day>29 || day<0){
            Console.WriteLine("wrong day");
            int wd = 0;
          }
        }
        else {
          if (day<0 || day>28){
            Console.WriteLine("wrong day");
            int wd = 0;
          }
        }
        if (year<0 || year>9999){
          Console.WriteLine("wrong year");
          int wy = 0;
        }

      }
    }
  }
}



专业程序员的编辑有这个功能和其他一些如括号匹配和语法高亮。

Notepad ++ Home [ ^ ]

ultraedit [ ^ ]


为什么要强行打开门?您应该使用正确的数据类型 - DateTime 。请参阅:



Why to force doors wide open? You should work with proper data type - a DateTime. See:

CultureInfo provider = CultureInfo.CreateSpecificCulture("en-US");
string format = "MM/dd/yyyy";
string stringDate;
DateTime myDate;

Console.Write("Enter date in the following format: '{0}'", format);
stringDate = Console.ReadLine();
try
{
	myDate = DateTime.ParseExact(stringDate, format, provider);
	Console.WriteLine("Correct date!");
}
catch (FormatException)
{
	Console.WriteLine();
	Console.WriteLine("'{0}' is NOT in the correct format.", stringDate);
}





详情请见: DateTime.ParseExact Method(String,String,IFormatProvider)(System) [ ^ ]


您的问题是因为你要重新声明你的变量。



Your issue is because you are re-declaring your variables.

// Declare a new variable named 'myInt'
int myInt;
// Populate the variable
myInt = 22;
// Repopulate the variable
myInt = 33;



一旦你声明了一个变量,它就可以在整个范围内使用,这取决于你声明它的位置。

例如,如果你在方法之外声明它,它可以在整个班级中使用。

如果你在一个方法中声明它,它在整个方法中都可用

如果你在if或using语句中声明它,那么它在整个if或using声明中是可用的但不在它之外。

下面的其他例子


Once you declare a variable it is available throughout the scope, which is determined where you declare it.
For instance, if you declare it outside of a method, it is available throughout the entire class.
If you declare it inside a method, it is available throughout the method
If you declare it inside of an if or using statement then it is available throughout if or using statement but not outside of it.
Additional Examples below

using System;
// declare a variable that can be accessed throughout any method within this class
private int ClassAccessibleInt = 12;

public static void Main(string[] args)
{
// declare a variable that can be accessed within the Main method
int MethodAccessibleInt = 10;
// declare another variable that can be accessed within the Main by adding 2 others
int AnotherMethodAccessibleInt = ClassAccessibleInt + MethodAccessibleInt;
// create an if statement
if(DateTime.Today.Month > 2)
{
    // declare another variable that can be accessed only within the If Statement
    int IfAccessibleInt = DateTime.Today.Month + ClassAccessibleInt;
    // add value to AnotherAccessibleInt
    AnotherMethodAccessibleInt = AnotherMethodAccessibleInt + IfAccessibleInt;
    // IfAccessibleInt = 3 + 12 = 15
    // ClassAccessibleInt = 12
    // MethodAccessibleInt = 10
    // AnotherMethodAccessibleInt = 10 + 12 + 3 + 12 = 37
    // close if statement
}
// IfAccessibleInt no longer available
// ClassAccessibleInt = 12
// MethodAccessibleInt = 10
// AnotherMethodAccessibleInt = 37
// end Main Method
}
// ClassAccessibleInt = 12
// MethodAccessibleInt no longer available
// AnotherMethodAccessibleInt no longer available





亲切的问候



Kind Regards


这篇关于如果所有日期都没有错,我需要打印一些东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆