小编Ave*_*ova的帖子

使用带有if else循环的try-catch

所以我正在创建一个程序,它从命令行获取一个int输入,然后在程序中搜索int中的数组,如果找到,返回数字的索引.如果没有,那么它会抛出一个异常,说明找不到它并结束程序.这是我到目前为止:

public static void main(String[] args) {

    int[] intArray = {9, 97, 5, 77, 79, 13, 7, 59, 8, 6, 100, 55, 35, 89, 74, 66, 32, 47, 51, 88, 23};
    System.out.println(Arrays.toString(intArray));

    int intArgs = Integer.parseInt(args[0]);

    System.out.println("Your entered: " + intArgs);

    FindNum(intArray, intArgs);
}

public static int FindNum(int[] intArray, int intArgs) {
    for (int index = 0; index < intArray.length; index++){
        try{
            if (intArray[index] == (intArgs))
                System.out.println("Found It! = " + index);
            else 
                throw new NoSuchElementException("Element not found in array."); …
Run Code Online (Sandbox Code Playgroud)

java arrays methods command-line try-catch

5
推荐指数
2
解决办法
1334
查看次数

在 Java 中显示带括号和逗号的数组输出?

我试图在我的程序中用括号和逗号打印数组。这是我的代码:

public static void main(String[] args) {

    int[] arrayIntList = new int[10]; // Starting the array with the specified length

    int sum = 0; // Defining the sum as 0 for now

    // Using the for loop to generate the 10 random numbers from 100 to 200, inclusive.
    for(int nums1 = 0; nums1 < arrayIntList.length; nums1++) {
        arrayIntList[nums1] = (int)(100 + Math.random()*101);
    }           

    Arrays.sort(arrayIntList); // Sorting the array list

    System.out.print("[");
    for(int i = 0; i < arrayIntList.length; i++) { // …
Run Code Online (Sandbox Code Playgroud)

java arrays formatting brackets comma

2
推荐指数
1
解决办法
4567
查看次数

要求用户在Python中输入正确的响应?

我是一名新手程序员,我正在尝试编写一个程序,我要求用户提供像奥巴马,克林顿或布什这样的特定输入,并在他们给出正确答案时向他们表示祝贺,或在他们给出错误答案时通知他们.

我很确定我犯了一个非常简单和愚蠢的错误,所以如果有人能帮助我,我会很感激.

def main ():

    pres = input ('Please enter the surname of a recent President of the United States: ')
    if pres == 'Bush' or 'Obama' or 'Clinton':
        print('Great job! You know your stuff!')
    else:
        print('Sorry, that was incorrect.')

main()
Run Code Online (Sandbox Code Playgroud)

谢谢!

python python-3.x

0
推荐指数
1
解决办法
2607
查看次数

在 Python 3.4.2 中打印一个倒三角形?

因此,对于我的编程课,我应该编写一个输出以下内容的程序:

oooooo
ooooo
oooo
ooo
oo
o
Run Code Online (Sandbox Code Playgroud)

我已经能够使程序正确输出三角形,但即使在网上寻找帮助后,我也无法找到将三角形颠倒的解决方案。这是我的代码:

def main():

base_size = 6

for r in range (base_size):
    for c in range (r + 1):
        print('o', end = '')
    print()

main()
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

o
oo
ooo
oooo
ooooo
oooooo
Run Code Online (Sandbox Code Playgroud)

有人可以帮我吗?我是新手,所以这对我来说有点困难,尽管对你们来说可能很简单。

提前致谢!

python loops python-3.x

0
推荐指数
1
解决办法
2万
查看次数