如何在主要方法中用流利的句子打印我创建的方法的结果?

  1. 我创建了一个名为showCharacter的方法。该方法获取一个字符串,并显示该字符串中给定位置的字符。这是该方法的代码:
Select 
    Equip,Count like Description,from
    WorkOrder (nolock) 
where 
    DateTm Between DATEADD(month,DATEDIFF(month,getDate()),0) and DATEADD(month,-1,-1)
Group by 
    Equip,Description 
order by 
    Equip Asc
  1. 主要,我想要求用户输入一个字符串,然后要求用户输入一个数字,该数字指定该字符串中的位置(第一个字母,第二个字母,第五个字母等)。

我遇到的问题毕竟是要打印到屏幕上的“在USER_ENTRY位置的字母是:CHARactER_FROM_THE_METHOD_I_CREATED_EARLIER。”

这是我主要方法中的当前代码:

    public static void showCharacter(String userStr1,byte charLoc)
    {
        System.out.println(userStr1.charAt(charLoc));
    }

我尝试写:

public static void main(String[] args) {
        // TODO code application logic here


        Scanner k = new Scanner(System.in);
        System.out.println("Please enter a String");
        String str = k.nextLine();
        int strLen = str.length();
        System.out.println("Please enter the character's position");
        byte i = k.nextByte();


        while (i <0 || i > (strLen -1))
        {
            System.out.println("Invalid Position. Enter a valid position");
            i = k.nextByte();
        }



        showCharacter(str,i);


    }

有什么想法吗?

谢谢!

cangmings 回答:如何在主要方法中用流利的句子打印我创建的方法的结果?

保持您的showCharacter方法几乎与最初编写的一样。

    public static void showCharacter(String userStr1,byte charLoc)
    {
        System.out.print(userStr1.charAt(charLoc));
    }

区别在于我使用print而不是println,因为我不想在假设字符必须是行中的最后一个东西的情况下进行连线。只需输出字符即可。

在拥有有效职位后的main中,进行输出:

    System.out.print("The letter at position " + i + " is ");
    showCharacter(str,i);
    System.out.println();

即,分为三个部分:

  1. 角色之前的东西
  2. 人物
  3. 字符后面的东西(这里除了行尾没有其他内容)
本文链接:https://www.f2er.com/3169770.html

大家都在问