- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用二维数组来表示使用(X,Y)坐标的网格。网格为 80 列 x 30 行。我正在使用重定向 (java project1 < project1.input) 为我的程序提供命令行。
这是project1.input的样子:
c
l 10 10 20 10 *
l 20 10 20 20 *
l 20 20 10 20 *
l 10 20 10 10 *
p 15 15 X
d
q
c = 清除(重新初始化)
l = 使用字符 ( * ) 从 X1 Y1 到 X2 Y2 绘制一条线
p = 使用字符 ( X ) 在 X1 Y1 处绘制一个点
d = 打印当前网格
q = 退出
这是我的整个.java,由于某种原因它只能绘制单个点而不能打印线条。我尝试查看我的 for 循环来设置点,我的 getInput 方法,但我找不到问题。
import java.util.Scanner;
import java.lang.*;
public class project1
{
// declaring class array named grid
public static char[][] grid = new char[80][30];
// method: main
// purpose: calls getInput and initGrid
public static void main(String[] args)
{
initGrid();
getInput();
}
// method: initGrid
// purpose: initializes the grid with spaces, also acts as the clear command
public static void initGrid()
{
char fill = '1';
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[0].length; j++){
grid[i][j] = fill;
}
}
}
// method: getInput
// purpose: gets the input and sends it to the appropriate method
public static void getInput()
{
char firstCharacter, drawCharacter;
int X1, Y1, X2, Y2;
Scanner in = new Scanner(System.in);
while(in.hasNextLine()){
firstCharacter = in.next().charAt(0);
if(firstCharacter == 'p'){
X1 = in.nextInt();
Y1 = in.nextInt();
drawCharacter = in.next().charAt(0);
in.nextLine();
plotPoint(X1, Y1, drawCharacter);
}
if(firstCharacter == 'l'){
X1 = in.nextInt();
Y1 = in.nextInt();
X2 = in.nextInt();
Y2 = in.nextInt();
drawCharacter = in.next().charAt(0);
in.nextLine();
drawLine(X1, Y1, X2, Y2, drawCharacter);
}
if(firstCharacter == 'c'){
in.nextLine();
initGrid();
}
if(firstCharacter == 'd'){
in.nextLine();
printGrid();
}
if(firstCharacter == 'q'){
return;
}
}
}
// method: plotPoint
// purpose: receives the p command and plots the point with given coordinates and char
public static void plotPoint(int X, int Y, char character)
{
grid[X][Y] = character;
}
// method: drawLine
// purpose: receives the l command and draws a line with given coordinates and char
public static void drawLine(int X1, int Y1, int X2, int Y2, char character)
{
int stopX = X1;
int stopY = Y1;
int stopX2 = X2;
int stopY2 = Y2;
// checks for horizontal line, left to right
if(Y1==Y2 && X1<X2){
for(int i = 0; stopX == stopX2; i++){
grid[X1+i][Y1] = character;
}
}
// checking for horizontal line, right to left
if(Y1==Y2 && X1>X2){
for(int i = 0; stopX2 == stopX; i--){
grid[X1+i][Y1] = character;
}
}
// checking for vertical line, top to bottom
if(X1==X2 && Y1<Y2){
for(int i = 0; stopY == stopY2; i++){
grid[X1][Y1+i] = character;
}
}
// checking for vertical line, bottom to top
if(X1==X2 && Y1>Y2){
for(int i = 0; stopY2 == stopY; i--){
grid[X1][Y1+i] = character;
}
}
}
// method: printGrid
// purpose: prints the grid to show the picture
public static void printGrid()
{
for(int j = 0; j < grid[0].length; j++){
for(int i = 0; i < grid.length; i++){
System.out.print(grid[i][j]);
if(i == grid.length-1){
System.out.println();
}
}
}
}
}
抱歉括号,它不能很好地从 emacs 中复制出来。
最佳答案
您的 for 循环不起作用,因为条件错误!
import java.util.Scanner;
import java.lang.*;
public class Grid
{
// declaring class array named grid
public static char[][] grid = new char[80][30];
// method: main
// purpose: calls getInput and initGrid
public static void main(String[] args)
{
initGrid();
getInput();
}
// method: initGrid
// purpose: initializes the grid with spaces, also acts as the clear command
public static void initGrid()
{
char fill = '1';
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[0].length; j++){
grid[i][j] = fill;
}
}
}
// method: getInput
// purpose: gets the input and sends it to the appropriate method
public static void getInput()
{
char firstCharacter, drawCharacter;
int X1, Y1, X2, Y2;
Scanner in = new Scanner(System.in);
while(in.hasNextLine()){
firstCharacter = in.next().charAt(0);
if(firstCharacter == 'p'){
X1 = in.nextInt();
Y1 = in.nextInt();
drawCharacter = in.next().charAt(0);
in.nextLine();
plotPoint(X1, Y1, drawCharacter);
}
if(firstCharacter == 'l'){
X1 = in.nextInt();
Y1 = in.nextInt();
X2 = in.nextInt();
Y2 = in.nextInt();
drawCharacter = '*';
in.nextLine();
drawLine(X1, Y1, X2, Y2, drawCharacter);
}
if(firstCharacter == 'c'){
in.nextLine();
initGrid();
}
if(firstCharacter == 'd'){
in.nextLine();
printGrid();
}
if(firstCharacter == 'q'){
return;
}
}
}
// method: plotPoint
// purpose: receives the p command and plots the point with given coordinates and char
public static void plotPoint(int X, int Y, char character)
{
grid[X][Y] = character;
}
// method: drawLine
// purpose: receives the l command and draws a line with given coordinates and char
public static void drawLine(int X1, int Y1, int X2, int Y2, char character)
{
// checks for horizontal line, left to right
if(Y1==Y2 && X1<X2){
for(int i = 0; i<= Math.abs(X2-X1); i++){
grid[X1+i][Y1] = character;
}
}
// checking for horizontal line, right to left
if(Y1==Y2 && X1>X2){
for(int i=02; i<=Math.abs(X2-X1); i++){
grid[X2+i][Y1] = character;
}
}
// checking for vertical line, top to bottom
if(X1==X2 && Y1<Y2){
for(int i = 0; i<=Math.abs(Y1-Y2); i++){
grid[X1][Y1+i] = character;
}
}
// checking for vertical line, bottom to top
if(X1==X2 && Y1>Y2){
for(int i = 0; i<=Math.abs(Y1-Y2); i++){
grid[X1][Y2+i] = character;
}
}
}
// method: printGrid
// purpose: prints the grid to show the picture
public static void printGrid()
{
for(int j = 0; j < grid[0].length; j++){
for(int i = 0; i < grid.length; i++){
System.out.print(grid[i][j]);
if(i == grid.length-1){
System.out.println();
}
}
}
}
}
试试这个!
关于Java : Drawing using a two dimensional array as a grid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21224401/
我正在尝试使用预先训练的模型。这就是问题发生的地方 模型不是应该接受简单的彩色图像吗?为什么它需要 4 维输入? RuntimeError T
我正在尝试实现自定义用户控件。 让我们考虑 ViewModels: public class FileViewModel { public string Name { get; set; }
我承认,我脑子有问题。我已经走得够远了,几乎可以看到隧道尽头的曙光,但我不确定要采取的下一步。 我创建了一个 SQLfiddle example here 这是 SQL 数据透视表: SET @sql
我收到 SSAS 错误“函数中指定的两个集合具有不同的维度”。 我实际上使用相同的维度,相同的层次结构(隐式),在一组中我使用“全部”成员,而在另一组中我使用叶成员。如果我更改成员标识符以显式引用(单
我有以下值,每个值都是 double 类型的标量:a1, a2, a3, a4, a5 . 我尝试使用 Numpy 连接它们,如下所示: f = np.concatenate((a1,a2,a3,a4
我有一个功能 findMaxEval我以以下方式调用:eMax0,var0=findMaxEval(np.diag(eVal0),q,bWidth=.01)哪里np.diag(eVal0)是一个形状为
我复制了spline example具有以下点(而不是随机点): var points = [[75, 15], [104, 80], [275, 80], [300, 15]]; 它工作完美。现在我
我正在尝试将值传递给后台 worker 。我之前发帖询问如何做到here 。一个答案将我引向了这段代码,但我在尝试使用它时遇到了问题。这就是我正在做的事情: Class MyParameters
问题是这样的:程序的输出是什么? #include int main() { int A[2][10]={{1,2,3,4,5,6,7,8,9,10},{11,12,13,14,15,16,17,1
我可以像这样内联创建一个 jquery 对象(这段代码有效) $('#tip').qtip({ content: el.REASON, position
我正在尝试模拟一维伊辛模型。该模型包含自旋链(100 次自旋),并使用 Mont Carlo - Metropolis 在系统能量(单一)下降或小于随机数时接受自旋翻转。在正确的程序中,能量和磁化强度
我试图从 Ruby 调用的 C 函数是这样的: void foo(double *in_array, double *out_array) 哪里: in_array 是一组数组,“foo”将使用这些数
我正在尝试校准加速度计,但我无法获得校准所需的 6 个不同加速度读数的 6 个样本值。 PreliminaryW 是一个 double[6][3] 数组,用于填充这些样本值。它是 6 x 3,因为每个
我很难想出一种有效的方法来找到二维容器中给定值的所有相邻方 block 。假设我有一个容器表示为: . . . . . . G . . . . . . . . . . . . . . . . . G
例如:一个二维数组可以想象成一堵方砖砌成的砖墙,其中每 block 砖代表我们数组中的一个坐标。 3 维数组可以同样的方式可视化为一个盒子或立方体。 但是,这是棘手的部分,您如何可视化具有多个(超过
我有以下代码通过 seaborn 创建一个表格和一个条形图。 #Building a dataframe grouped by the # of Engagement Types sales_type
我在 excel 中有一系列单元格,左列有人名,顶行有某些任务(作为数字),比如两个暗淡的数组。我想做的就是能够搜索名称和编号的任务,以查看该单元格在 VBA 中是否为空白。 在 excel 表中,我
问题总结:自定义函数的backward pass中如何处理输入和输出的维度? 根据manual ,自定义函数的基本结构如下: class MyFunc(torch.autograd.Function)
我一直在用 Javascript 开发一个项目,在达到一定的开发阶段后,代码停止工作。我已将问题范围缩小到在 Javascript 中创建和索引“多维”数组。我包含的代码只是为了测试创建数组的数组、为
package arrays; import java.util.Arrays; public class Route { int cityindex; int stadtwahl; String[]
我是一名优秀的程序员,十分优秀!