我最近注册了码战。我正在执行我的第一个java kata,我在代码上出现了一个奇怪的错误,在我看来是有效的。以下是代码:
import java.math.BigInteger;
public class Powers {
public static BigInteger powers(int[] list) {
BigInteger number;
if (list.length = 0){
number = 1;
}else if (list.length = 1){
number = 2;
}else if (list.len
我在一个类中编写了一个方法,它从main中检索一个数组和一个设置值。其目的是定位给定值的位置并将其返回给main (如果数组中不存在该值,则返回-1)。对于我的问题:当我运行我的代码时,Java说: int不能转换为boolean。并将我指向下面代码中的if语句。我尝试将"="-sign切换为">=",然后它运行得很流畅(但它的工作方式并不像它应该的那样……)。有人知道为什么它认为我想把它转换成布尔值吗?
public static int containsIntElement(int[] A, int val)
{
int
如果有人能解释一下为什么会发生以下情况,我将不胜感激。非常感谢。
boolean b = true;
// Compiles OK.
// The LHS "assignment operand" requires no ()parentheses.
if (b=true || b==true);
// Reverse the ||'s operands, and now the code doesn't compile.
if (b==true || b=true);
// Add () around the RHS "assignment ope
Im无法运行以下代码并获得以下消息(箭头指向模数符号):
错误:意外类型if ((数字% 10) =0&(数字%9 =0)) {System.out.print(“您赢了$200");} ^必需:变量找到:值
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
if
下面给出的If逻辑在C语言中运行良好,但在java....Why中不能运行。??
它在编译时在java中给出一个错误。
class test
{
public static void main(String[] args)
{
int i;
if(i=4)
System.out.println("hello");
}
}
代码:
import java.util.Scanner;
public class testqu {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Enter the length of the first side of the triangle");
double a = console.nextInt();
我现在有下面的java代码。
public class lesson8
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
String user;
int length, counter, spacecounter;
spacecounter=0;
c.print("Enter a string.
我正在创建一个Java程序,将数组中的天分配给一年中的天,所以我需要在七天后重新初始化我的数组。如何在for-loop块中重新初始化"I“变量。 public static void mainAction() {
for (int i = 0; i < day.numberOfDays; i++) {
if (day.dayNum == (i + 1))
day.day = DAYS[i];
if (dayNum == 7)
day.day = 1;
}
} 这段代码一直在运行
来自的报价
ConditionalExpression在ECMAScript中的语法与C和Java中的语法略有不同,后者允许第二个子表达式为表达式,但将第三个表达式限制为ConditionalExpression。ECMAScript中这种差异的动机是允许一个赋值表达式由条件的任何一个分支来管理,并消除逗号表达式作为中心表达式的混乱和相当无用的情况。
有谁能用例子更详细地解释一下差异吗?useless case of a comma expression as the centre expression是什么意思?为什么C/Java中的条件表达式不是allow an assignmen
import java.io.*;
public class AdamHmwk4 {
public static void main(String [] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int counter1;
int counter2;
int counter3;
String answer = "";
在Java中
int a=10;
a = a + ++a;
System.out.println(a);
它打印21。我知道它会打印22
我知道因为'++‘具有更高的优先级,所以它将首先被计算出来,并且它将改变a的值,因为它是增量前的值,所以变量'a’的增量将出现在那里,而then...later则应该添加一个最新的值。
如下所示:
a = a + 11; // (a is 11 after pre - increment)
所以,现在是a = 11 + 11 = 22,但是程序会生成o/p = 21。
意味着它不是选择一个最新的值,即11,而是使用旧的值,即10
a = 10
我有一个关于Java中三值运算符的问题。我收到一个错误:
> Compilation error Main.java:14: error: unexpected type
> (i%2==0) ? sum += i/2 : sum += i/2+1;
> ^ required: variable found: value 1 error
我不明白为什么?谢谢!
import java.util.*;
public class Main {
public static void main(String[] args) {
我最近一直在使用Java的byte原语,我遇到了一个愚蠢的问题:
byte a = 10;
byte b = 9;
byte c = 8;
b += b*c; // how come this statement is correct without any explicit type casting
b = b*c; // while this statement is incorrect; it requires explicit cast, of course
b = b+(b*c); // this is wrong too.
所以我的问题是,除了加法和赋值之外,+=是否
嗨,我的c#已经不是以前的样子了,在使用java脚本一段时间后又回来了。本质上,我只是尝试使用数组的索引来执行一个简单的if语句,但我收到了错误消息。
"Operator '||' cannot be applied to operands string and string"
为什么这是不允许的,因为它本质上变成了一个布尔值。
string[] userCustomAnswerArray = {"It needs to be reaplaced", "This could be improved", &
可能重复:
在编译时,我发现了Java强类型检查中的不一致性。请查看以下代码:
int sum = 0;
sum = 1; //is is OK
sum = 0.56786; //compile error because of precision loss, and strong typing
sum = sum + 2; //it is OK
sum += 2; //it is OK
sum = sum + 0.56787; //compile error again because of automatic conversion into double, and possibl
在下面的代码中,为什么Java认为简单的名称numCows可能没有被初始化?为什么使用限定-这可以防止错误?
import java.util.function.Function;
public class CowFarm {
private final int numCows;
public CowFarm(int numCows) {
this.numCows = numCows;
}
// Fails to compile
//
// CowFarm.java:12: error: variable numCows
在我的主要活动中:
count=count+1;
//the problem seems to be in this line of code below
String fileName=f.getFileName(count).toString();
// the method below invokes a method from another class. it works fine so please ignore
helloTxt.setText(readText.readTxt(fileName));
F是一个FileName类的对象,我从其中获取文件名:
public clas
ArangoDB3-3.7.2_win64 arangodbJavaDriver=6.7.5 假设集合有100多个文档,下面的AQL查询使用AqlQueryOptions().fullCount(true)通过Java驱动程序返回stats.fullCount中的期望值: FOR a IN SomeCollection FILTER a.field = @p1 SORT a.field ASC LIMIT 0,100 RETURN a 但使用IN运算符的类似查询中,stats.fullCount的值意外为100: FOR a IN SomeCollection FILTER a.field
当我尝试编译下面的代码时,我一直收到错误,我目前使用的是JCreator。
import java.io.*;
public class Number //class name here, same as file name
{
public Number()throws IOException{//constructor, place class name here
// use BufferedReader class to input from the keyboard
// declare a variable of type BufferedReader
Buffere
我刚刚注意到,虽然我在向map添加对时需要澄清括号,但在进行重新赋值时不需要它们:
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Server VM, Java 1.6.0_18).
Type in expressions to have them evaluated.
Type :help for more information.
给我一些值
scala> var n = Map.empty[Int, String]
n: scala.collection.immutable.Map[Int,String] = Map(
+=的定义在Java和C++中似乎都是相同的,但是它们的执行情况不同。
考虑C++中的以下代码:
#include <iostream>
int n;
int f(int x) {
n += x;
return x;
}
int main() {
n = 0;
n = n + f(3);
std::cout<<n<<" ";
n = 0;
n += f(3);
std::cout<<n<<" ";
n = 0;
n = f