- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要两个子程序createArray()
和print()
。 Print()
将需要 createArray()
中的 multArray
变量,并且我已经编写了程序,以便数组不会在 main 中本地创建。我意识到我可以将 createArray
设置为 createArray(int a, int b)
但我决定不这样做。现在这会回来咬我吗?还是有办法让我在不进行建议的更改的情况下完成此任务?
import java.util.*;
import java.io.*;
public class Array {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String newLine = System.lineSeparator();
String choiceInput;
boolean stop = false;
boolean firstTime = true;
while (stop == false){
if (firstTime == true) {
System.out.println("Welcome To The Multiplications Table Creator!" + newLine + newLine + "Would you like to:" + newLine + newLine + "Create Print Exit" + newLine + newLine + "Please enter one of the above in the space below: ");
}
else {
System.out.println("Welcome Back!" + newLine + newLine + "Would you like to:" + newLine + newLine + "Create Print Exit" + newLine + newLine + "Please enter one of the above in the space below: ");
}
choiceInput = scan.nextLine().toUpperCase();
if (choiceInput.equals("CREATE")) {
createArray();
firstTime = false;
for (int count = 0; count < 10; count++) {
System.out.println(newLine);
}
}
else if (choiceInput.equals("PRINT")) {
print();
firstTime = false;
}
else if (choiceInput.equals("EXIT")) {
for (int count = 0; count < 10; count++) {
System.out.println(newLine);
}
System.out.print("Thank you for using the program!");
for (int count = 0; count < 2; count++) {
System.out.println(newLine);
}
stop = true;
}
else System.out.println("You did not enter one of the above!");
}
}
public static int[][] createArray() {
Scanner s = new Scanner(System.in);
String newLine = System.lineSeparator();
int a;
int b;
System.out.print("How big would you like your multiplication table to be? (A x B)" + newLine + "A: ");
a = s.nextInt();
System.out.println(a + " x ");
b = s.nextInt();
int[][] multArray = new int[a][b];
for (int countA = 1; countA <= a; countA++) {
for (int countB = 1; countB <= b; countB++) {
multArray[countA - 1][countB - 1] = countA * countB;
}
}
System.out.print("Creating .");
delay(1000);
System.out.print(" .");
delay(1000);
System.out.print(" .");
delay(1000);
System.out.println(newLine + "Done.");
return multArray;
}
public static void print() {
**//This is where I need to print multArray created above is it possible?**
}
public static void delay(int millis) {
try {
Thread.sleep(millis);
}
catch (InterruptedException exp) {
}
}
}
最佳答案
您的 createArray
方法返回一个 int[][]
数组,因此您可以执行类似的操作
int[][] multiArray = createArray();
//将create array方法的结果存储在multiArray中
现在更改您的 print
方法以接受 int[][]
数组,如下所示
public static void print(int[][] multiArray);
//接受 int[][] 数组作为参数的打印方法
当您调用类似这样的打印方法时,将 multiArray
传递给打印方法
print(multiArray)
//将 createArray 的早期结果(multiArray)传递给 print 方法。
现在,在 print 方法中,您可以打印 multiArray
。
关于java - 有没有办法在 createArray() 的子程序 print() 中打印 multArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40300951/
我有一个包含很多工作表和几个宏的工作簿。当我进入 VBA 并尝试将新的 Sub 写入 ThisWorkbook 模块时,我看到: "This will reset your project, proc
1、函数定义 子程序即一段分离的代码,它可以使减少重复代码且程序易读.perl中,子程序可以出现在程序的任何地方.但一般放在程序的开始或结尾. 复制代码 代码如下:
1、定义 子程序即执行一个特殊任务的一段分离的代码,它可以使减少重复代码且使程序易读。PERL中,子程序可以出现在程序的任何地方。定义方法为: &n
如何将 deck(52) 数组从 Newgame 函数传递到 deckshuffle 函数 FUNCTION newgame 'New game RANDOMIZE TIMER CA
有没有办法在后台运行 perl 子程序?我环顾四周,看到了一些关于线程的提及,但看到一个例子会有所帮助,或者为我指明正确的方向。谢谢。 想跑run_sleep在后台。 #!/usr/bin/perl
情况 我正在创建一个简单的模板文件,该文件将有助于创建 future 的脚本,以便在 *nix 系统上通过命令行执行各种任务。作为其中的一部分,我可能会要求用户输入需要根据源代码中提供的正则表达式进行
我想将以下变量传递给子程序 mySubroutine,$name, $age然后是这个多维数组: $name = "jennifer"; $age = 100; $list[0][0] = "TEST
据我所知,VB6不支持继承,但它支持接口(interface)。我正在尝试创建一个重载子例程,将其信息传递给基类的同名子例程。 Sub Main() Dim Student1 as New S
这个问题已经有答案了: Dynamic Function Calls in Excel VBA (1 个回答) 已关闭 8 年前。 这是我的测试代码 Sub dotask() Dim qusu
我正在编写一个本质上是静态的函数。我想将它插入到模板工具包中,它会传递类名。本质上,它正在做 ClassName->function( $args.. ) 但我希望它做类似的事情 ClassName:
我创建了一个小示例程序来检查子例程系统调用。 package main func print() { } func main() { go print() } go 子程序的 straces
我是该网站的新手,这看起来可能是获得一些提示和帮助(如果有的话)的地方。 我正在学习“C 调用 Fortran 子程序”,我对 C 有一定的了解,但对 Fortran 了解不多。 优点:我看过一些例子
是否有一种方法/功能可以为所有可用的 Mojolicious 路由编写自动启动子程序/方法? 也许是一个自动助手,但我还不知道如何去做。 我认为这对于为几乎所有可用路由初始化数据库连接 $self->
我试图在不实例化对象的情况下从类中调用原型(prototype)函数。我的类(class) MyClass 的一个例子: package MyClass; use strict; use warnin
我正在尝试从 C 调用 FORTRAN 函数 我的问题是: 如果 fortRoutine 是我的 fortran 子例程的名称,那么我从 C 调用它作为 fortRoutine_。如果 fortRou
我可以调用编译这个 fortran 代码 'test.f90' subroutine test(g,o) double precision, intent(in):: g double precisi
我制作了一个 Perl 模块 MyModule.pm 它有一些我想在 shell 脚本中调用的子例程 getText。我尝试了以下方式,但它给出了错误; SEC_DIR=`perl -MMyModul
我用 CommaIde 打开了这个简单的脚本: #!/usr/bin/env perl6 my $str = 'foobar'; say $str; IDE 突出显示了带有错误的“说”一词: Subr
我基本上有一个存储有数字 1-6(例如垄断)的立方体 vector > cube; 看起来像这样: 0300 5126 0400 我有将它倒转的代码: short tmp=cube[0][1]; cu
我必须在两个文件中创建一个 surbroutine,我在构建项目时遇到问题,出现错误: undefined reference to c 我不知道发生了什么,我正在尝试发送 C[0] 内存方向,这就是
我是一名优秀的程序员,十分优秀!