好吧所以我是ArrayList的新手,我想做的是制作一个程序,从一副54张卡中获得3张随机卡,没有任何重复.我不知道在我的if循环中放什么.请帮忙
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.ArrayList;
public class card_ran1 extends JFrame
{
public card_ran1()
{
ArrayList<Integer> Ran = new ArrayList<Integer>();
setLayout(new GridLayout(1,4,5,5));
Random random = new Random();
int i = random.nextInt(54) + 1 ;
int n = random.nextInt(54) + 1 ;
int m = random.nextInt(54) + 1 ;
Ran.add(i);
if (Ran.contains(n))
{
//what should go here
}
if (Ran.contains(m))
{
//what should go here
}
add(new JLabel(new ImageIcon("card/" + Ran.get(0) + ".png")));
add(new JLabel(new ImageIcon("card/" + Ran.get(1) …Run Code Online (Sandbox Code Playgroud) 我正在编写一个代码,在一个名为primes.txt的文件中写入2到1000之间的所有素数.出于某种原因,我无法找出解决此问题的正确方法.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Problem6 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
PrintWriter prw = new PrintWriter("primes.txt");
for (int i = 2; i <= 1000; i++){
if (checkIfPrime(i) == true){
System.out.println(i);
prw.println(i);
}
}
}
public static boolean checkIfPrime (int num){
boolean isPrime = true;
for (int i = 2; i <= 1000; i++){
if ( num % i == 0 ){
isPrime = false; …Run Code Online (Sandbox Code Playgroud) public class SumAndAverage {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
int num;
for(num = 0; num <= 100; num++){
sum += num;
}
double average = ((double)sum) / num;
System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
总和是5050,平均是50.0
怎么了:
5050/100 = 50.5不是50.0
首先,这是我的代码中有问题的部分; 这些都是非常基础的课程
public Passenger(String Name, String adress, String number, String password){
count++;
accId+=count;
this.Name=Name;
this.adress=adress;
this.number=number;
if(checkPw(password)==true){
this.password=password;
}
}
private boolean checkPw(String password){
int length;
length = password.length();
if(length != 6){
return false;
}
else if(password.charAt(0)==0){
return false;
}
else {
for (int i = 0; i < password.length();i++){
if((password.charAt(i))==(password.charAt(i+1))){
return false;
}
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
识别TestClass:
public static void main(String[] args){
Passenger gokhan=new Passenger("Gokhan","Istanbul","xxx","254651");
System.out.println(gokhan.password);
}
Run Code Online (Sandbox Code Playgroud)
所以,我认为问题出在Passenger类.这是我第一次在课堂上上课(我的意思是if(checkPw(密码)== true)部分).在测试类中,它看起来非常清晰,我从未想过会出现此错误.我该如何避免这条消息?
完整错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index …Run Code Online (Sandbox Code Playgroud) 我正在使用 C 语言编写一个将参数作为输入的程序。是否可以检查您的 argv 是否包含特定的文件扩展名?就像检查 argv 中是否有任何 char* 以 .jpeg 或类似的结尾?(任何示例代码将不胜感激:))
我见过的每个关于强力算法的例子都有指数运行时间.
这是一个严格的规则,即所有暴力算法在运行时是指数的吗?
我C之前在SO上看了一篇帖子.我忘记了它的含义,但绝对不是我想在这里问的问题 - sizeof.
在评论部分的2个回答者之间进行了争论.这是两者之间激烈的辩论,但一个解释从未明确说过.
我试图找到它两天,但我找不到它,因此我的问题在这里.并且谷歌搜索没有产生任何关于此的结果.
焦点: sizeof()或sizeof ()或只是sizeof
所以这些人基本上有两种类型:
第一组:
一位学者以里奇的名义发誓,因为他试图用以下方式登记他的车牌而从不会驾驶汽车:
sizeof ()政府之间sizeof和()政府之间的空间(在某些国家/地区()- 允许使用车牌)将不允许数字/字母之间的空白空间.
第二组:
每隔一段时间,一位带着扎染衬衫的装饰C老兵会在回答你的时候回答你:"我真的不认识男人,有什么区别?我甚至都不知道这笔交易是什么.我甚至不记得..那些常春藤Lea ...... sizeof(),sizeof ()或者只是sizeof,我不担心,伙计."
那么,应该sizeof写一个sizeof与它之间的空间()甚至没有()或者真的没关系?
我欢迎来自C专家和学者的解释.那些不太适合上述类型的人 - 自学成才的CBobs和Marys--在他/她的手臂上纹有"C99和Ritchie"也受到欢迎.
它不会伤害我任何方式,但我只是好奇是否是一个"标准",在一个空间之间sizeof,()甚至一个没有().
或者他们真的一样 - 对我来说会很有趣.
所以我的问题是我需要这些方法一个接一个地运行,但我无法弄清楚如何使方法在运行之前等待.任何帮助表示赞赏.谢谢.这是我的代码:
public void startMoving() throws InterruptedException
{
moveEnemy("right",3);
wait();
moveEnemy("down",3);
wait();
moveEnemy("right",2);
wait();
moveEnemy("up",1);
wait();
moveEnemy("right",2);
wait();
moveEnemy("up",2);
wait();
moveEnemy("right",2);
wait();
moveEnemy("down",4);
wait();
moveEnemy("left",1);
wait();
moveEnemy("down",2);
wait();
moveEnemy("right",3);
wait();
moveEnemy("up",2);
wait();
moveEnemy("right",1);
wait();
moveEnemy("up",1);
wait();
moveEnemy("right",3);
}
public void moveEnemy(final String direction, final int numMoves)
{
Thread moveThread = new Thread(new Runnable()
{
public void run()
{
isMoving = true;
int originalX = getX();
int originalY = getY();
for(int loop = 0; loop <= 98*numMoves; loop++)
{
try
{
Thread.sleep(5); …Run Code Online (Sandbox Code Playgroud) 我需要Eclipse/Maven专家的一些帮助 - 我无法将我的项目视为在Eclipse中部署到Tomcat的资源.所以我做了一个搜索,发现如果我运行Maven - > Update Project,那就可以缓解这个问题.但是,更新导致以下错误:
An internal error occurred during: "Updating Maven Project".
org.eclipse.wst.common.componentcore.internal.impl.ReferencedComponentXMIResourceImpl cannot be cast to org.eclipse.jpt.common.core.resource.xml.JptXmlResource
Run Code Online (Sandbox Code Playgroud)
我不知道问题是什么.
我一直试图在这段代码中找到错误的位置已经有一段时间了,我无法弄明白.该程序将6面模具滚动300次,然后输出每个数字滚动的时间.但出于某种原因,它不是滚动300次,而是滚动320次.我没有看到for循环有什么问题,所以我真的不知所措.
public static void dieRoll(){
int[] roll = new int [300];
int[] count = new int[] {1,2,3,4,5,6};
for(int i = 1; i<300; i++){
roll[i] = (int) Math.ceil( (int) (Math.random()*6)+1 );
// roll[i] = (int) Math.ceil(roll[i]);
// System.out.println(roll[i]);
if(roll[i]==1){
count[0]++;
}
else if(roll[i]==2){
count[1]++;
}
else if(roll[i]==3){
count[2]++;
}
else if(roll[i]==4){
count[3]++;
}
else if(roll[i]==5){
count[4]++;
}
else if(roll[i]==6){
count[5]++;
}
// System.out.println(roll[i]);
}//i loop
System.out.println("The die landed on 1 " + count[0] + " times.");
System.out.println("The die landed on 2 …Run Code Online (Sandbox Code Playgroud)