我有一项任务,可以找到用户输入的最小,最大和平均数.基本上,它们输入正整数,由空格分隔,Java滚动它们并将它们相加.我能够找到总和,平均值和最大整数,但是,我无法找到最小的整数.我认为解决这个问题的最好方法是将表示最小int的变量设置为等于表示循环外部最大int的变量.然后,在循环中,执行以下操作:
if(getInt < min)
{
min = getInt;
}
Run Code Online (Sandbox Code Playgroud)
其中getInt是用户输入的值,min是最小整数值.但是每次我运行它时,min都会返回0.
这是我的完整代码:
import java.util.Scanner;
public class exc5
{
public static void main (String[] args)
{
System.out.println("Write a list of nonnegative integers, each seperated by a space. To signal the end of the list, write a negative integer. The negative integer will not be counted");
Scanner keyboard = new Scanner (System.in);
keyboard.useDelimiter(" |\n");
int count = 0;
int sum = 0;
int max = 0;
int min = max;
double average = …Run Code Online (Sandbox Code Playgroud) 我有一个下拉列表,在页面加载时填充,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
ddlCars.Items.Add("Ford");
ddlCars.Items.Add("Chevy");
ddlCars.Items.Add("BMW");
ddlCars.Items.Add("Jeep");
ddlCars.Items.Add("Nissan");
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但是当用户更改下拉列表的选择时,列表会重新填充,我会看到相同的项目两次,因为页面正在重新加载并且正在重新添加这些值.
什么是阻止这种情况发生的最好方法?重要的是AutoPostBack保持启用状态,以便根据用户选择更改信息.
谢谢
我有一个 ASP.NET 项目的作业,该项目处理从 global.asax 文件存储和检索全局变量。
我的老师给了我这部分来定义变量:
Application.Add("Name", "Andrew");
Run Code Online (Sandbox Code Playgroud)
但在那之后,他有点让我悬而未决。所以我想知道是否有人愿意为我指出处理这些全局变量的正确方向。具体来说,用于在我的应用程序的不同页面中调用变量。
我试图在字符串中找到每个ASCII字符的出现,并用新行替换它.这是我到目前为止:
public string parseText(string inTxt)
{
//String builder based on the string passed into the method
StringBuilder n = new StringBuilder(inTxt);
//Convert the ASCII character we're looking for to a string
string replaceMe = char.ConvertFromUtf32(187);
//Replace all occurences of string with a new line
n.Replace(replaceMe, Environment.NewLine);
//Convert our StringBuilder to a string and output it
return n.ToString();
}
Run Code Online (Sandbox Code Playgroud)
这不会添加新行,并且字符串全部保留在一行中.我不确定这里的问题是什么.我也试过这个,但结果相同:
n.Replace(replaceMe, "\n");
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我一直在通过Python工作,但似乎无法通过字符串比较.我写了一个函数,它接受用户输入并对其进行评估.用户输入只能是"a"或"b",否则会发生错误.我一直在用这个:
def checkResponse(resp):
#Make the incoming string trimmed & lowercase
respRaw = resp.strip()
respStr = respRaw.lower()
#Make sure only a or b were chosen
if respStr != "a" | respStr != "b":
return False
else:
return True
Run Code Online (Sandbox Code Playgroud)
但是,当我输入a或者b,我收到这个:TypeError: unsupported operand type(s) for |: 'str' and 'str'
这是比较字符串的错误方法吗?是否有像Java一样的内置函数?谢谢!
我有一个包含18个对象的数组,并且数组被分配为包含25个对象(其余7个对象为null以供将来使用).我正在编写一个打印出所有非空对象的程序,但是我正在运行NullPointerException,我无法弄清楚如何绕过它.
当我尝试这个时,程序崩溃了Exception in thread "main" java.lang.NullPointerException:
for(int x = 0; x < inArray.length; x++)
{
if(inArray[x].getFirstName() != null)//Here we make sure a specific value is not null
{
writer.write(inArray[x].toString());
writer.newLine();
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,程序运行,但仍然打印空值:
for(int x = 0; x < inArray.length; x++)
{
if(inArray[x] != null)//Here we make sure the whole object is not null
{
writer.write(inArray[x].toString());
writer.newLine();
}
}
Run Code Online (Sandbox Code Playgroud)
有人能指出我正确的方向来处理数组中的空对象吗?所有帮助表示赞赏!