我正在尝试计算字符串中特定字符的出现次数,但输出错误.
这是我的代码:
inputString = str(input("Please type a sentence: "))
a = "a"
A = "A"
e = "e"
E = "E"
i = "i"
I = "I"
o = "o"
O = "O"
u = "u"
U = "U"
acount = 0
ecount = 0
icount = 0
ocount = 0
ucount = 0
if A or a in stri :
acount = acount + 1
if E or e in stri :
ecount = ecount + 1
if I …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个程序,它会一直询问用户一个整数,直到他们输入一个非整数的值,此时程序停止.
这是我到目前为止:
import java.util.Scanner;
import java.util.ArrayList;
public class InputStats {
private static Scanner a;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList InputArray = new ArrayList();
Boolean Running = true;
System.out.println("Please type an integer. To stop enter a non integer value");
while (Running) {
a = Scanner.nextLine();
if (!a.hasNextInt()) {
Running = false;
}
else {
InputArray.add(input.nextLine());
}
System.out.println(InputArray);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,使用此代码我收到错误:
error: non-static method nextLine() cannot be referenced from a static context (for the …
Run Code Online (Sandbox Code Playgroud) 代码为什么会这样
print("Average =" (sum/count))
Run Code Online (Sandbox Code Playgroud)
产生类型错误而不是语法错误,看到逗号丢失了吗?
谢谢.
我试图从每个元素之间具有不同宽度的元组输出字符串.
这是我目前使用的代码:
b = tuple3[3] + ', ' + tuple3[4] + ' ' + tuple3[0] + ' '
+ tuple3[2] + ' ' + '£' + tuple3[1]
print(b)
Run Code Online (Sandbox Code Playgroud)
比方说,我输入这些文本行:
12345 1312 Teso Billy Jones
12344 30000 Test John M Smith
Run Code Online (Sandbox Code Playgroud)
输出将是这样的:
Smith, John M 12344 Test £30000
Jones, Billy 12345 Teso £1312
Run Code Online (Sandbox Code Playgroud)
如何保持填充与3个部分之间较大的间距一致?
此外,当我直接从文本文件输入这些字符串时,这是我收到的输出:
Smith
, John M 12344 Test £30000
Jones, Billy 12345 Teso £1312
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
非常感谢.
我正在尝试创建一个函数,该函数使用类型为BasicAsteroid的(x,y,velocity x,velocity y)值生成随机小行星,这是创建随机小行星的构造函数和函数:
private double x, y;
private double vx, vy;
public BasicAsteroid(double x, double y, double vx, double vy) {
this.x = x;
this.x = y;
this.vx = vx;
this.vy = vy;
}
public static BasicAsteroid makeRandomAsteroid() {
Random rand = new Random();
BasicAsteroid x = new BasicAsteroid((rand.nextInt()%FRAME_WIDTH), (rand.nextInt()%FRAME_HEIGHT), (rand.nextInt()%MAX_SPEED), (rand.nextInt()%MAX_SPEED));
System.out.println(x);
return x;
}
Run Code Online (Sandbox Code Playgroud)
然而,这是我创建小行星时的输出:
game1.BasicAsteroid@6773120a
game1.BasicAsteroid@4261b6b3
game1.BasicAsteroid@2673b915
game1.BasicAsteroid@113eb90b
game1.BasicAsteroid@1abcc522
如何输出值而不是类@saskcode?
谢谢.