问题描述
每次我尝试运行该程序时都会说这个名为`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
推荐答案
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;
}
}
}
}
}
专业程序员的编辑有这个功能和其他一些如括号匹配和语法高亮。
[]
[]
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);
}
详情请见: []
// 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
这篇关于如果所有日期都没有错,我需要打印一些东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!