在Java中打印ABC…和数字的问题

huangapple go评论83阅读模式
英文:

Problem of printing ABC... and a number in Java

问题

这是我设置的函数:

public int printABCNum(int p) {
    char textinfo = 65;
    String textValue = "";
    while (textinfo < 91) {
        textValue += textinfo;
        textinfo++;
    }
    System.out.println(textValue + Integer.toString(p));
}

Eclipse(一个文本编辑器)表示public int printABCNum(p)这部分有问题。基本上我尝试的是运行一个打印"ABCDEFGHIJKLMNOPQRSTUVWXYZ"和一个整数的函数。我对Java还不太熟悉,对于使用它进行编程和查找错误不够有把握,所以请帮忙检查一下!

英文:

This is the function I set:

public int printABCNum(p) {
	char textinfo = 65;
	String textValue = &quot;&quot;;
		while (textinfo &lt; 91) {
			textValue+=textinfo;
			textinfo++;
		}
        System.out.println(textValue + Integer.toString(p));

Eclipse (a text editor) says that there is a problem with public int printABCNum(p) bit. Basically I'm trying to do is run a function printing "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and an integer. I'm new to Java and is not good 'enough' at programming with it and finding bugs, so please help!

答案1

得分: 2

一个 printABCNum 函数的 p 参数缺少类型说明。根据上下文,它应该是一个 int 类型。另一个问题是你的函数返回类型被定义为 int,但是你并没有返回任何值。

你的代码应该是:

public void printABCNum(int p) {
    char textinfo = 65;
    String textValue = "";
    while (textinfo < 91) {
        textValue += textinfo;
        textinfo++;
    }
    System.out.println(textValue + p);
}
英文:

A p parameter of your printABCNum function is missing a type. Based on context it should be an int. Another problem is your function return type is defined to int, but you haven't returned any value.

Your code should be:

public void printABCNum(int p) {
    char textinfo = 65;
    String textValue = &quot;&quot;;
    while (textinfo &lt; 91) {
        textValue += textinfo;
        textinfo++;
    }
    System.out.println(textValue + p);
}

答案2

得分: 1

看起来有一些问题。

  1. 如果你不打算返回任何东西,就不应该有返回类型int
  2. 参数需要有类型,像这样:int p

除此之外看起来还不错。在下面看一下对我有用的完整答案。

public void printABCNum(int p) {
    char textinfo = 65;
    String textValue = "";
    while (textinfo < 91) {
        textValue += textinfo;
        textinfo++;
    }
    System.out.println(textValue + Integer.toString(p));
}
英文:

It looks like there's a few things wrong here.

  1. You shouldn't have a return type int if you are not returning anything.
  2. The parameter needs to have a type, as such: int p

Other than that looks fine. Take a look below at full answer that worked for me.

public void printABCNum(int p) {
    char textinfo = 65;
    String textValue = &quot;&quot;;
    while (textinfo &lt; 91) {
        textValue+=textinfo;
        textinfo++;
    }
    System.out.println(textValue + Integer.toString(p));
}

huangapple
  • 本文由 发表于 2020年8月25日 19:30:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63577893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定