<script Language="c#" runat="server">
void Page_Load()
{
DateTime date = DateTime.Now;
dateToday.Text = " " + date.ToString("d");
DayOfWeek day = DateTime.Now.DayOfWeek;
dayToday.Text = " " + day.ToString();
if ((dayToday == DayOfWeek.Saturday) && (dayToday == DayOfWeek.Sunday))
{
Console.WriteLine("This is a weekend");
}
}
</script>
使用dateTime,我试图测试当前日期是工作日还是周末,然后我想将响应打印给用户。目前我收到运行时错误。如果我删除了我的if语句,则第一项(当前日期和星期几)打印正确。
答案 0 :(得分:34)
你在以下if语句中写错了varable:
if ((dayToday == DayOfWeek.Saturday) || (dayToday == DayOfWeek.Sunday))
{
Console.WriteLine("This is a weekend");
}
而不是 dayToday ,您必须在条件中使用 day varable。
<强>更新强>
你在条件上犯了错误。必须有or
而不是and
。
答案 1 :(得分:6)
您正在将ASP.NET标签import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class ArrayWith10Numbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Integer[] myArray = new Integer[10];
System.out.print("Enter " + myArray.length + " values: ");
for (int i = 0; i < 10; i++) {
myArray[i] = input.nextInt();
}
Set<Integer> set = new HashSet<>();
set.addAll(Arrays.asList(myArray));
System.out.println("Without duplicates");
for (Integer val : set)
System.out.println(val);
}
}
与dayToday
的枚举元素进行比较,这当然是失败的
您可能希望在DayOfWeek
声明中将dayToday
替换为day
,即来自
if
到
if ((dayToday == DayOfWeek.Saturday) && (dayToday == DayOfWeek.Sunday))
此外,您可能还想更换逻辑&#39;和&#39; (if ((day == DayOfWeek.Saturday) && (day == DayOfWeek.Sunday))
)与逻辑&#39;或&#39; (&&
)到最后
||
答案 2 :(得分:1)
if ((day >= DayOfWeek.Monday) && (day<= DayOfWeek.Friday))
{
// action
}
答案 3 :(得分:1)
您收到错误是因为您要将枚举与字符串进行比较。
// dayToday is a string
// DayOfWeek.Saturday is an enum
if ((dayToday == DayOfWeek.Saturday) && (dayToday == DayOfWeek.Sunday))
使用DayOfWeek.Saturday.ToString()
与字符串进行比较。您还希望不填充dayToday
字符串。或者,使用day
变量与枚举进行比较。
https://dotnetfiddle.net/gUGJ0J
using System;
public class Program
{
public static void Main()
{
DateTime date = DateTime.Now;
string dateToday = date.ToString("d");
DayOfWeek day = DateTime.Now.DayOfWeek;
string dayToday = day.ToString();
// compare enums
if ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday))
{
Console.WriteLine(dateToday + " is a weekend");
}
else
{
Console.WriteLine(dateToday + " is not a weekend");
}
// compare strings
if ((dayToday == DayOfWeek.Saturday.ToString()) || (dayToday == DayOfWeek.Sunday.ToString()))
{
Console.WriteLine(dateToday + " is a weekend");
}
else
{
Console.WriteLine(dateToday + " is not a weekend");
}
}
}
答案 4 :(得分:0)
您需要将asp控件放在form标签内,并使用runat =“server”。
<body>
<form id="frm" runat="server">
<p>
Today's date is: <asp:Label ID="dateToday" runat="server" />
<br/>
The day of the week is: <asp:Label ID="dayToday" runat="server" />
<br/>
</form>
</body>
代码中Page_Load
方法的签名不正确。此外,您应该将Response.Write
用于asp网站而不是Console.Writeline
。当您检查星期几时,应该有OR(||)而不是AND(&amp;&amp;)。
<script language="c#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
DateTime date = DateTime.Now;
dateToday.Text = " " + date.ToString("d");
DayOfWeek day = DateTime.Now.DayOfWeek;
dayToday.Text = " " + day.ToString();
if ((dayToday.Text == DayOfWeek.Saturday.ToString()) || (dayToday.Text == DayOfWeek.Sunday.ToString()))
{
Response.Write("This is a weekend");
}
}
</script>
答案 5 :(得分:0)
List<DateTime> datelist = new List<DateTime>();
int balanceday = 1;
while (datelist.Count < 10)
{
DateTime day = DateTime.Now.AddDays(balanceday + datelist.Count).Date;
if (day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday)
{
datelist.Add(day);
}
else
{
balanceday++;
}
}