给定一个A10 的数组ints,初始化一个被调用的局部变量,sum并使用一个循环来查找数组中所有数字的总和A.
这是我提交的答案:
sum = 0;
while( A, < 10) {
sum = sum += A;
}
Run Code Online (Sandbox Code Playgroud)
我对这个问题没有任何意见.我做错了什么?
所以我刚开始用Java编写第二个编程类,这是教授给我们演示循环和数组的例子.
public class ArraysLoopsModulus {
public static void main(String [ ] commandlineArguments){
//Declare & Instatiate an Array of 10 integers
Integer[ ] arrayOf10Integers = new Integer[10];
//initialize the array to the powers of 2
Integer powersOf2 = new Integer(1);
for(int i=0;i<arrayOf10Integers.length;i++){
arrayOf10Integers[i] = powersOf2;
//multiply again by 2
powersOf2 = powersOf2 * 2;
}
//display the powers of 2
System.out.println("The first 10 powers of 2 are: ");
for(int i=0;i<arrayOf10Integers.length;i++){
System.out.print(arrayOf10Integers[i] + ", ");
}
}
}
Run Code Online (Sandbox Code Playgroud)
看了所有即将到来的例子,似乎我的教授从不使用原始数据类型,他总是使用等效的对象类(在这种情况下Integer代替int …
我们正在进行小组作业.该计划的一部分用于输入"支付",然后输入学生的SIN(社会保险号)以及扣除其账户的金额.例如,pay,193678187,900.
将SIN与来自文件的SIN列表进行比较,生成数组并进行比较,curSin.equals(stuSin)). curSin并且stuSin都是字符串.
如果找到SIN,它将减少学生余额(未显示)的数量并将其写入文件write(student,inrec).
如果未找到SIN,则应返回"未找到SIN".
我们的问题是,即使找到了SIN,它仍然会返回"未找到SIN".我们认为它与if语句和不等于比较有关,但我们已经尝试了所有我们能想到的并且无法使其正常工作.
任何帮助将不胜感激.谢谢.
下面的代码片段.
if (cmd.equals("pay") && p.length == 3)
{ // first word is "pay";three terms (cmd, sin, $)
System.out.println("pay");
// Pay: decreases student payment, notifies of overpayment
String stuSin = p[1];
double stupay = Double.parseDouble(p[2]);
for (int i = 0; i < student.length; i++)
{
Student x1 = student[i];
String curSin = x1.getSin();
if (curSin.equals(stuSin))
{
double curpay = x1.getBal();
double newBal = curpay - …Run Code Online (Sandbox Code Playgroud) 我试图在java中创建一个简单的基于文本的计算器我的第一个程序,EVER,我无法弄清楚如何将输入String转换为变量opOne.然后我会尝试numOne反对numTwo使用opOne作为运营商.代码如下:
import java.io.*;
import java.math.*;
public class ReadString {
public static void main (String[] args) {
System.out.print("Enter the first number: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numOne = 0 ;
int numTwo = 0 ;
String opOne = null;
while(true){
try {
numOne = Integer.valueOf(br.readLine());
break;
} catch (IOException error) {
System.out.println("error; try again.");
System.exit(1);
}
catch (NumberFormatException nfe) {
System.out.println("error;try again.");
}
}
System.out.print("Enter the …Run Code Online (Sandbox Code Playgroud) 我的天啊.我有一个小项目要做,字符串正在杀了我!
现在,我有一个String null(取自getParameter()servlet 调用的值).
问题是,我试图看看它是否为null,并且,即使它为null,在程序中告诉我不是null,但是稍后在程序中,当我使用变量时,我收到一个异常说变量是null.
System.out.println("In " + ID); // in console: In null
if ((ID == null) || (ID == "null") || ID.equals(null) || **ID.equals("null")**)
{
// after I put the 4th condition, the if is working right (WHAT IS THE PROBLEM?)
System.out.println("==null");
this.ID = "";
}
else
{
System.out.println("!=null");
this.ID = ID;
}
System.out.println("After " + ID);
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
只有第四个条件正在发挥作用!剩下的事情(除了第二个,因为我说的这个条件是因为我绝望了)
我教过ID == null或 ID.equals(null)将会好,但不是.
编辑:
问题是,我从表单中获取ID的值(表格1通常说 - 通常).但在这种情况下,我使用的表单2没有任何ID输入,因此ID必须是null …
所以我在递归的最后一部分上遇到了一些麻烦.该方法需要使用递归来返回一个字符串,该字符串由"编织"在一起形成两个作为参数的字符串.例如:
weave("aaaa", "bbbb") // should return the string "abababab"
weave("hello", "world") // should return the string "hweolrllod"
weave("recurse", "NOW") // should return the string "rNeOcWurse"
Run Code Online (Sandbox Code Playgroud)
请注意,第一个字符串中的额外字符 - "urse"中的字符 - 在编织在一起的字符之后.
重要的(也是令人讨厌的)是我不允许使用任何迭代循环(for,while,do while).
这是我到目前为止的代码:
public static String weave(String str1, String str2)
{
String word = str1 + str2;
if(str1 == null || str1.equals("") || str2 == null || str2.equals(""))
{
return word;
}
String word1 = weave(str1.substring(0, str1.length() - 1), str2.substring(0, str2.length() - 1));
System.out.println(word1);
return word;
}
Run Code Online (Sandbox Code Playgroud)
对于(Hello,World),我的输出是:
HW
HeWo …Run Code Online (Sandbox Code Playgroud) 使用以下代码获取错误:
import java.util.Scanner;
public class FahrenheitToCelsius{
public static void main(String[]args){
convertFToC();
}
/*gets input representing fahrenheit and displays celsius equivalent*/
public static void convertFToC(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter Fahrenheit temperature");
double fahrenheit = scan.nextInt();
System.out.println(fahrenheit + " degrees Fahrenheit is " +
fMethod(celsius) + " degrees Celsius");
}
/* calculates and returns the celsius equivalent */
public static double toCelsius(double fahr){
int BASE = 32;
double CONVERSION_FACTOR = 9.0/ 5.0;
double celsius = ((fahr-BASE)/(CONVERSION_FACTOR));
return celsius;
}
} …Run Code Online (Sandbox Code Playgroud) 我试图在eclipse中创建如下所示的程序,但它显示错误.我是java的新手,所以有人可以帮助我修复它并说明它出现错误的原因吗?
public class Specifiers {
public static void main(String[] args) {
public void start1()
{
System.out.println("In Start Method");
}
}
}
Run Code Online (Sandbox Code Playgroud) 所以我试图学习递归(我知道在这种情况下递归是没有必要的)
我已经写过这个方法,它有效
public static int method(int number) {
if(number == 0) {
return 1;
}
else {
return (int)Math.pow(2,number) + method(number-1);
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于将2的幂从0加到数字,但我想知道是否有一种方法来替换Math.pow()另一个递归方法调用
我正在逐字节地读取数据.当我确定我有一个完整的消息时,我需要将它作为字符串传递给另一个函数.有些消息可能非常大,但大小经常变化.您觉得哪种实现最有效:
public test class
{
char[] buffer = new char[MAX_SIZE_7200];
int bufferIndex = 0;
void parseData(ArrayList<Byte> msg, length)
{
while (!msg.isEmpty())
{
buffer[bufferIndex++] = (char) msg.remove(0);
if (isfullmessage)
{
parseData(new String(buffer, 0, bufferIndex);
bufferIndex = 0; //restart and continue parsing data
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要么:
public test class
{
List<Character> buffer = new ArrayList<Character>();
int bufferIndex = 0;
void parseData(ArrayList<Byte> msg, length)
{
while (!msg.isEmpty())
{
buffer.add((char) msg.remove(0));
if (isfullmessage)
{
StringBuilder builder = new StringBuilder(buffer.size());
for (Character ch: …Run Code Online (Sandbox Code Playgroud)