编写一个方法来打印字符串“Name”多次

发布于 2024-11-01 16:49:38 字数 416 浏览 5 评论 0原文

我正在回答编程入门课程的问题,但在读完我的笔记、Big Java 书并上网查找后,无法找到从哪里开始。

看起来好像应该很简单,但我只需要开始。所有这些都是针对 Java 的,并且正在 Eclipse 中进行。

任务是接收一个名称(“Name”)和一个数字(int x),并在一行上显示“Name”x 次,在另一行上显示 x-1 次,依此类推,直到您仅显示一次名称。看起来它应该是一个反向累加器,但我在启动我的方法时遇到问题。我该如何开始?我知道我不能像在 python 或其他语言中那样在 Java 中乘以字符串,但是如何在不构建数组或输入的情况下打印“名称”x 次

System.out.println("name" + " " + "name" + " "...).

任何建议都值得赞赏。我是这方面的新手。谢谢你!

I am answering a question for an Intro to Programming class and cannot - after reading through my notes, Big Java book, and looking online - find out where to begin.

It seems as though it should be very simple, but I just need to get started. All of this is for Java and is being worked on in Eclipse.

The task is to take in a name ("Name") and a number (int x) and display the "Name" x number of times on one line, x-1 times on another line, and so on and so on until you display the name only once. It seems like it should be a reverse accumulator, but I'm having trouble starting my method. How do I begin? I know I can't multiply strings in Java like you can in python or other languages, but how can I print the "Name" x number of times without building an Array or inputing

System.out.println("name" + " " + "name" + " "...).

Any suggestions are appreciated. I am a novice at this. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

东京女 2024-11-08 16:49:38

您需要一个循环,这是编程语言的常见功能。

仔细看看 java for 循环。

附加提示:

System.out.println("test") 在一行中打印其参数,而 System.out.print("test") 不添加测试后的CR/LF

You need a loop, a common feature of programming languages.

Have a close look at the java for loop.

Additional hint:

System.out.println("test") prints it's argument in a single line while System.out.print("test") doesn't add a CR/LF after test.

您应该阅读java 流控制语句forwhile 结构将允许您做您想做的事情。

You should read about java flow control statements. The for and while constructs would allow you to do what you want.

我们的影子 2024-11-08 16:49:38

您的方法获取一个包含名称和计数的字符串。使用一个 for 循环进行计数,并使用另一个 for 循环重复名称值,您应该能够获得所需的输出。

System.out 是一个打印流,它有 println() 来输出以换行符结尾的行,还有 print() 来输出字符串而不以换行符结束。

Your method gets a string with the name and a count. Using one for loop to count and another to repeat the name value, you should be able to get the output you're after.

System.out is a printstream and it has both println() to output a line ended with a linebreak and print() to output a string without ending it with a linebreak.

吖咩 2024-11-08 16:49:38

您可以使用 System.out.print 和 System.out.println 来完成此操作。

这是示例代码:(不是您想要的实际代码,只是一个示例)

import java.io.*;

public class StringNameTest
{
    public static void main(String args[])
    {
            String strNumber = "";
            String strName = "";
            int intNumber;
            try 
            {
                //  open up standard input
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                //  prompt the user to enter their name
                System.out.print("Enter your name: ");
                strName = br.readLine();

                System.out.print("Enter the number of times you want to print your name: ");
                strNumber = br.readLine();
                intNumber = Integer.parse(strNumber);

                for (row = 0; row < intNumber; row++)
                {
                    for(col = 0; col < intNumber; col++)
                        System.out.print(strName + " ");

                    System.out.println("");
                }
            } 
            catch (Exception ex) {
                System.out.println(ex.getMessage());
                System.exit(1);
            }


    }
}

希望这有帮助。

You can do this by using System.out.print and System.out.println.

Here is the sample code: (not the actual code which you want, just a SAMPLE)

import java.io.*;

public class StringNameTest
{
    public static void main(String args[])
    {
            String strNumber = "";
            String strName = "";
            int intNumber;
            try 
            {
                //  open up standard input
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                //  prompt the user to enter their name
                System.out.print("Enter your name: ");
                strName = br.readLine();

                System.out.print("Enter the number of times you want to print your name: ");
                strNumber = br.readLine();
                intNumber = Integer.parse(strNumber);

                for (row = 0; row < intNumber; row++)
                {
                    for(col = 0; col < intNumber; col++)
                        System.out.print(strName + " ");

                    System.out.println("");
                }
            } 
            catch (Exception ex) {
                System.out.println(ex.getMessage());
                System.exit(1);
            }


    }
}

Hope this helps.

深府石板幽径 2024-11-08 16:49:38

使用计数到 x 的外循环和计数到 (x - 外循环计数器) 的内循环。请注意常见的“一次性”错误。

Use an outer loop that counts to x and an inner loop that counts to (x - outer loop counter). Be aware of the common "one off" error.

静若繁花 2024-11-08 16:49:38

Java 没有内置的字符串重复是正确的。但您可以定义自己的方法来完成此操作。

public static String repeated(String s, int times) {
  StringBuilder sb = new StringBuilder();
  // TODO: append /s/ to /sb/ for /times/ times.
  return sb.toString();
}

对于您来说,您想要使用这个“运算符”,而不是将所有内容都放在一大块代码中,这对您来说是个好点。这有助于保持程序的可读性。

It is correct that Java doesn't have built-in String repetition. But you can define your own method that does exactly this.

public static String repeated(String s, int times) {
  StringBuilder sb = new StringBuilder();
  // TODO: append /s/ to /sb/ for /times/ times.
  return sb.toString();
}

Good point for you that you want to use this "operator" instead of putting together everything into one large block of code. This helps to keep your programs readable.

思念满溢 2024-11-08 16:49:38

第一个想法:您需要 n 行。所以,进入一个循环。

for (int i = 0; i < n; ++i)
{

然后:您需要在第 i 行打印字符串 n - i 次。输入嵌套循环:

     for (int j = 0; j < n - i; ++j)
     {

然后开始打印名称:

           System.out.print(name + " ");
     }

注意我没有使用 println,因为我们希望所有名称都在同一行。
现在,当我们打印 n - i 次时,您需要结束该行:

     System.out.println();

关闭我们的外循环:

}

如果您不知道如何让程序询问名称和整数 n给用户:

String name = null;
int n = 0;
try
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter the name: ");
    name = br.readLine();
    System.out.print("How many times (n): ");
    n = Integer.parseInt(br.readLine());
} catch (Exception e)
{
    e.printStackTrace();
    System.exit(-1); // exit!, there was an error
}

First thought: you need n lines. So, enter a loop.

for (int i = 0; i < n; ++i)
{

Then: you need to print on line i the string n - i times. Enter a nested loop:

     for (int j = 0; j < n - i; ++j)
     {

Then start off printing the name:

           System.out.print(name + " ");
     }

Notice I didn't use println, because we want all the names on the same line.
Now you need to end the line when we printed n - i times:

     System.out.println();

Close our outer-loop:

}

And if you don't know how to make the program ask the name and the integer n to the user:

String name = null;
int n = 0;
try
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter the name: ");
    name = br.readLine();
    System.out.print("How many times (n): ");
    n = Integer.parseInt(br.readLine());
} catch (Exception e)
{
    e.printStackTrace();
    System.exit(-1); // exit!, there was an error
}
强辩 2024-11-08 16:49:38
public class test{

    public static void main(String[] args){

        int i = 0;
        int j = 20;

   for(i = 1; i <= j; i++){

          System.out.println("My Name");


   } //ends for loop

     // use system.out.print("") for same line 


}    //ends static void main

}
public class test{

    public static void main(String[] args){

        int i = 0;
        int j = 20;

   for(i = 1; i <= j; i++){

          System.out.println("My Name");


   } //ends for loop

     // use system.out.print("") for same line 


}    //ends static void main

}
我三岁 2024-11-08 16:49:38
int x;
int y = 1;

x = scanner.nextInt();

for (int i = 1; i <= x; i++) {
    for (int j = 1; j <= y; j++) {
        System.out.print("O");
    }
    System.out.println();
    ++y;
}
int x;
int y = 1;

x = scanner.nextInt();

for (int i = 1; i <= x; i++) {
    for (int j = 1; j <= y; j++) {
        System.out.print("O");
    }
    System.out.println();
    ++y;
}
千里故人稀 2024-11-08 16:49:38
public class WithoutLoop
{
      static int count;
   public static void repeat(String s)
   {
       while(count <= 10)
       {
           System.out.println(s);
           count++;
       }
   }
   public static void main(String[] args)
   {
        repeat("hello");
   }
}
public class WithoutLoop
{
      static int count;
   public static void repeat(String s)
   {
       while(count <= 10)
       {
           System.out.println(s);
           count++;
       }
   }
   public static void main(String[] args)
   {
        repeat("hello");
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文