Java 选择字符串的特定部分

Java 选择字符串的特定部分,java,arrays,string,Java,Arrays,String,我是编程新手,目前正在学习Java入门。我本周的作业要求我们以特定的方式打印课程列表。这是我的代码: package u9a1_defineclassinstantiateobj; import java.util.Scanner; /** * * @author Staci */ public class U9A1_DefineClassInstantiateObj { /** * @param args the command line arguments

我是编程新手,目前正在学习Java入门。我本周的作业要求我们以特定的方式打印课程列表。这是我的代码:

package u9a1_defineclassinstantiateobj;

import java.util.Scanner;

/**
 *
 * @author Staci
 */
public class U9A1_DefineClassInstantiateObj {
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Staci's Copy");
        
        Scanner input = new Scanner(System.in);
        
        String[][] courses = {
            {"IT1006", "IT4782", "IT4789", "IT4079", "IT2230", "IT3345", "IT2249"},
            {"6", "3", "3", "6", "3", "3", "6"}              
        };
        
            
        System.out.println("course Objects each has a code (e.g. IT1006) and credit hours (e.g. 6)");
        System.out.println("The number inside the [] is the display number");
        System.out.println("The number inside the () is the credit hours for the course");
        for(int i = 0; i < courses[0].length; i++ ) 
            System.out.println("[" + (i+1) + "]" + courses[0][i] + "(" + courses[1][i] + ")");
        
    }
    
}
包u9a1\u定义分类安装对象;
导入java.util.Scanner;
/**
*
*@作者Staci
*/
公共类U9A1_定义分类安装对象{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
System.out.println(“Staci的副本”);
扫描仪输入=新扫描仪(System.in);
字符串[][]课程={
{“IT1006”、“IT4782”、“IT4789”、“IT4079”、“IT2230”、“IT3345”、“IT2249”},
{"6", "3", "3", "6", "3", "3", "6"}              
};
System.out.println(“课程对象各有一个代码(如IT1006)和学时(如6)”;
System.out.println(“[]内的数字是显示编号”);
println(“()中的数字是课程的学时”);
对于(int i=0;i
我需要的不是每行的所有课程编号,而是每行的第一个课程(IT1006),而不改变[]和()编号。我觉得事情很简单,但我就是想不出来。谢谢你的帮助和指导

输出:


不要在输出中每行定义每门课程,只应定义课程IT1006,而行号和课程学分保持不变。

如果我理解您的问题,您只需将
课程[0][I]
更改为
课程[0][0]
即可

System.out.println("[" + (i + 1) + "]" + courses[0][0] + "(" + courses[1][i] + ")");
然后输出

Staci's Copy
course Objects each has a code (e.g. IT1006) and credit hours (e.g. 6)
The number inside the [] is the display number
The number inside the () is the credit hours for the course
[1]IT1006(6)
[2]IT1006(3)
[3]IT1006(3)
[4]IT1006(6)
[5]IT1006(3)
[6]IT1006(3)
[7]IT1006(6)
我发现对格式化io进行推理比较容易,以防我误解了您的请求,或者只是为了让它更容易阅读——我会这样做

System.out.printf("[%d]%s(%s)%n", i + 1, courses[0][0], courses[1][i]);

然后,您可以调整
[]
()
,而无需对输出变量进行额外转义。

如果只需要第一个数据,请删除循环并添加:

System.out.println("[" + (1) + "]" + courses[0][0] + "(" + courses[1][0] + ")");

[1]IT1006(6)
您想要的是
(IT1006)
???是吗?请对代码和代码片段、结构化文档(如HTML/XML或输入/输出)使用代码格式。为此,请选择文本并单击邮件发布/编辑表单顶部的
{}
按钮。另外,不要包含IDE插入的代码注释。它们只是噪音。是的!我知道事情很简单,我只是盯着它看。非常感谢。