public class Test{
public void newMethod(){
if(true)int i=0;
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了以下错误
Test.java:4: error: '.class' expected
if(true)int i=0;
^
Run Code Online (Sandbox Code Playgroud)
但如果我这样写
public class Test{
public void newMethod(){
if(true){
int i=0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
那就没有错误!
我知道这个问题对社区没有帮助,但我真的很好奇为什么我需要在这个声明中有括号.我已经用java编程了几年,我刚刚遇到这个错误.
顺便说一句,我正在使用JGrasp.
import java.util.Scanner;
public class Hw2JamesVaughn {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = input.nextInt();
if((year < 1582) == (year % 4==0))
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
if((year > 1582) == (year % 100 != 0) || (year % 400 == 0))
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
}
} …Run Code Online (Sandbox Code Playgroud) 嘿,我有一个问题,我一直试图找出几个小时,我需要使用嵌套循环打印以下
-----1-----
----333----
---55555---
--7777777--
-999999999-
Run Code Online (Sandbox Code Playgroud)
这就是我到目前为止所拥有的.
public static void Problem6 () {
System.out.println("Problem 6:");
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print("-");
}
for (int j = 1; j <= 9; j += 2) {
System.out.print(j);
}
for (int j = 5; j >= i; j--) {
System.out.print("-");
}
System.out.println();
}
}
Run Code Online (Sandbox Code Playgroud)
这就是它打印的内容
-----13579-----
----13579----
---13579---
--13579--
-13579-
Run Code Online (Sandbox Code Playgroud) 我是java的新手,我正在试图弄清楚数学函数是如何工作的.我无法弄清楚我错过了什么.
这是整个计划:
public class Math {
public static void main(String args[])
{
double x = Math.abs(4);
System.out.println(x);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,jGRASP说,"Math.java:5:错误:找不到符号double x = Math.abs(4);"
对不起,我知道这个答案很明显,但我想我只是很慢.任何人都可以给我一个实例变量和构造函数的明确定义吗?
实例变量:
您的类需要一个名为numbers的实例变量,它是一维数组.数组的大小将由构造函数确定.您可能会或可能不会发现其他实例变量很有用.
构造函数:
您将编写两个构造函数.一个构造函数将是没有参数的默认构造函数.它将创建一个包含10个int的数组,并将数组的每个元素设置为42.
第二个构造函数将接受一个参数,该参数将是一个int数组.此构造函数将创建与参数大小相同的实例数组,然后将参数中的整数复制到实例数组.
我不知道如何开始.
import java.util.Arrays;
public class example
{
private int numbers[]; //instance variable
private String result;
public example()
{
numbers = new int[10];
Arrays.fill(numbers, 42);
}
public example(int[] array)
{
numbers = Arrays.copyOf(array, array.length);
}
//get and set methods
public void setNumbers (int numbers)
{
//setMethod
this.numbers = new int[numbers];
}
public int [] getNumbers()//get method
{
return numbers;
}
public String toString()
{
String result = new String();
int i;
for (i = 0; i < numbers; i++)
result = result …Run Code Online (Sandbox Code Playgroud) 我可以使用MinGW编译C代码没问题,但由于某种原因我无法编译C++.我已经尝试过其他编译器,cygnus和cygwin.我正在使用JGrasp.这是错误提要:
----jGRASP wedge2 error: command "g++" not found.
---- This command must be in the current working directory
---- or on the current PATH to use this function.
---- PATH is ";C:\Program Files\Common Files\Microsoft Shared\Windows Live;
C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live
C:\windows\system32;
C:\windows;C:\windows\System32\Wbem;
C:\windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files (x86)\Windows Live\Shared;
C:\mingw\bin;
C:\cygnus\cygwin-b20\H-i586-cygwin32\bin;
C:\cygwin\bin;".
----jGRASP: operation complete.
Run Code Online (Sandbox Code Playgroud) 我正在尝试让给定的数组x(由用户在另一个方法中输入)包含重复值的方法(重复)返回true。否则它将返回false。而不是检查初始化为100的整个数组,它只会检查输入的值的数量,并使用全局计数器numElementsInX来跟踪该值。
做到这一点的最佳方法是什么?
public static boolean duplicates (int [] x)
Run Code Online (Sandbox Code Playgroud)
我提示输入用户数据,如下所示:
public static void readData (int [] x, int i){
Scanner input = new Scanner(System.in);
System.out.println("Please enter integers, enter -999 to stop");
while (i <= 99) {
int temp = input.nextInt();
if(temp == -999){
break;
}
else {
x[i++]=temp;
}
// else
}//end while
printArray(x,i);
}//end readData
public static void printArray(int [] x, int numElementsInX){
int n = numElementsInX;
for (int i = 0; i < n; i++){
System.out.print(x[i] + " …Run Code Online (Sandbox Code Playgroud) 为什么这个代码在else紧随其后的情况下有效if:
if (GPA > 3.8);
else
System.out.println("words");
Run Code Online (Sandbox Code Playgroud)
但是,如果我在"if"和"else"之间添加一个声明,例如:
if (GPA > 3.8);
System.out.println("words");
else
System.out.println("words");
Run Code Online (Sandbox Code Playgroud)
我得到一个错误说else需要一个if?
我正在做这个小程序,但不幸的是我遇到了这个问题..
if (ccnString.charAt(0) != '4' || ccnString.charAt(0) != '3') {
System.out.println("The String entered does not fit any of the Credit card standards");
System.exit(0);
}
Run Code Online (Sandbox Code Playgroud)
我的程序无法识别我是否在我的String中输入任何整数.
但是,如果我删除了我的|| 最后一部分,if语句识别第一个整数.
我在这里错过了什么?
这是我的语法.我试图显示这些值的平均值.它一直给我一个错误,上面写着:错误C2064:术语不评估函数.
这是我的代码:
#include <iostream.h>
int main()
{
double value;
double sumofvalue = 0;
int numberofvalues = 0;
const int sentinel = 0;
while(value!=sentinel){
cout << "Enter a value (0 to quit): ";
cin >> value;
numberofvalues++;
sumofvalue+=value;
}
cout << "Average is "((sumofvalue)/(numberofvalues));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
pubic String toString()
{
String str;
str = "RentalDays:" + RentalDays +"\nRenalRate:" + RentalRate + "\nVehicleID " + VehicleID;
return str;
}
Run Code Online (Sandbox Code Playgroud)
RentalDays,RentalRate和VehicleID都是代码中的整数
该javac编译器会发出此错误:
Tractor.java:67: error: ';' expected
pubic String toString()
^
Tractor.java:67: error: invalid method declaration; return type required
pubic String toString()
^
2 errors
Run Code Online (Sandbox Code Playgroud)
我对下一步该做什么一无所知,我想我需要覆盖tostring方法来显示int但我不确定如何,任何提示?
我想得到一些关于将整数转换为单词的帮助.我是Java的新手,我有点想到我将要做的事情,但需要一些帮助.
代码应打印0-999的数字,并在扫描仪中输入-1,程序应停止.
像这样:
请输入0到999之间的数字:1
一
请输入0到999之间的数字:11
十一
请输入0到999之间的数字:122
一百二十二
请输入0到999之间的数字:1000
数量超出范围
请输入0到999之间的数字:-1
感谢您使用我们的计划
我还必须使用方法来使这个"清洁"