【问题标题】:Multiple returns from a single int来自单个 int 的多个返回
【发布时间】:2015-03-15 20:50:15
【问题描述】:

我一直在尝试为二十一点制作记分方法(哦,是的,有 100 多页代码可供参考,没有我要查找的部分)

我想要的只是返回一个 int 数组,一个 1 到 11 之间的值,然后重复它,返回一个不同的随机值。

我已经尝试了几十种方法,我要么不够熟练,无法使其工作,要么该方法不适合我的其余代码。

这是我最近尝试的。

import java.util.Random;
import java.util.Scanner;

class jacktest
{
    public static void main ( String[] args )
    {
            Random r = new Random();

            Scanner input = new Scanner(System.in);
            String name;
            boolean playing = true;
            boolean notplaying = true;
            int ncard = 1 + r.nextInt(11);

            for (i=0; i>11; ++i);

            System.out.println("two cards from the same int: "+ncard +ncard);
    }
}

谁能帮我省点儿苦头,向我解释一下,为什么 ncard + ncard 在两次出现时都产生相同的数字?

或者什么是获得相同结果的更好方法?

谢谢

PS:我想让程序说类似,

你的第一张牌是 8 你的第二张牌是 4 总共 12 个 H 或 S(击打或粘住) 命中:5 总计:17 H 或 S(击打或粘住) 小号 坚持17岁 经销商转向:

所以总数会被记住,并根据hs 输入而增加。

我在另一个程序中看到过这种类型的代码,但它不适合我有限的编码技能。我需要一些非常简单且易于操作的东西。

【问题讨论】:

  • ncard 是一个始终具有相同值的变量,除非您对其进行修改。您只需将其设置在代码中的一处。这是你卡住的部分吗?

标签: java random int


【解决方案1】:

for 语句的工作方式是重复其块中的内容。

 int ncard = 1 + r.nextInt(11);
 for (i=0; i>11; ++i);  // get rid of this semicolon, the smeicolon creates a block
 System.out.println("two cards from the same int: "+ncard +ncard);

如果它是这样的

 int ncard = 1 + r.nextInt(11);
 for (i=0; i>11; ++i)  // indent next line to make it look nice
     System.out.println("two cards from the same int: "+ncard +ncard);

根本不运行,因为 i 永远不会大于 11。一个更合理的

 int ncard = 1 + r.nextInt(11);
 for (int i=0; i<11; ++i){
     System.out.println("two cards from the same int: "+ncard +ncard);
 }

现在实际上为每次重复获得一个新的 ncard 你应该这样做

 for (i=0; i<11; ++i){
     int ncard = 1 + r.nextInt(11);
     System.out.println("two cards from the same int: "+ncard +ncard);
 }

如果你这样做了,现在扩展你的编辑

 int card1 = 1 + r.nextInt(11);
 int card2 = 1 + r.nextInt(11);

你可以,之后创造条件

 if(card1 + card2 <= 17){
     // don't hit.
 }

【讨论】:

  • 可爱!!!!非常感谢 Mr.Giraffe :) 我做了一个 PS:上面,你能帮我吗?
  • 我不介意这是否可怕 :) 只要我今晚能入睡,哈哈。 RealSkeptic 编辑了我的编辑,使其更具可读性。帖子#1
  • @VincentTyson 您还需要另一个变量来处理总和。您的程序需要重复用户输入。
  • 好的,我对代码进行了更改,现在它没有返回任何内容,这是因为 int i=0; ??
  • @VincentTyson 这可能是一个新问题。
【解决方案2】:

你的循环结束因为你的分号,并在循环中输入你的随机和打印:

class jacktest
{
    public static void main ( String[] args )
    {
            Random r = new Random();

            Scanner input = new Scanner(System.in);
            String name;
            boolean playing = true;
            boolean notplaying = true;


            for (i=0; i>11; ++i) {
                int ncard = 1 + r.nextInt(11);
                System.out.println("two cards from the same int: "+ncard +ncard);
            }


} }

【讨论】:

  • 对。我想我有足够的工作,我会在这里发回任何打嗝。谢谢小伙伴们的支持。找不到符号.....应该是 int i =ncard; ???或者别的什么
  • 我应该在哪里以及如何声明“ i ”,无论我把它放在哪里,代码要么完全失败,要么找不到符号。
  • 我刚刚标记了你,重复这个问题,当我使用 for 语句时,我得到错误找不到符号 i,我已经尝试了我能想到的一切,但仍然没有快乐。你能帮忙吗?
  • 哎呦我的错,你需要声明i的类型,你可以在for: for (int i = 0...让我知道它工作
  • 是的,编译得很好,当我运行程序时,只有 system.out.println 是空白的。 i 和 nCard 之间似乎没有任何关系。我想说 int nCard = i;或类似的东西,我接近了吗?
猜你喜欢
  • 2014-04-10
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 2021-12-13
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多