我正在尝试创建一个程序,该程序接收可变数量的一行矩阵(也是可变长度)并将它们加在一起。因此,数组1的元素之一添加到数组2的元素之一,依此类推。
function add() {
var x = document.getElementById("input").value;
var z = x.split(" ");
var l = z[0].length;
var y = [];
y.length = l;
var i = 0;
var j = 0;
for(j = 0; j<l;j++){
for(i = 0; i < z.length; i++){
y[j] += z[i][j];
}
}
document.getElementById("output1").innerHTML = y;
}
</script>
<input type="text" id = "input" value = "233,322,444">
<button onclick = "add();"> add </button>
<div id = "output1"></div>
这是我试图开始工作的一些代码。数组产生NAN,NAN..Until数组的结尾。任何帮助将不胜感激。
另一个常见的输出例如是undefined233,undefined322,undefined444。这是我输入233,322,444之后的结果。
Also all columns must be same length i.e z[0] is length of all sets of numbers
答案 0 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setFrameVisibility(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.colourred) {
getWindow().getDecorView().setBackgroundColor(Color.RED);
}
return super.onOptionsItemSelected(item);
}
将是一串文字。如果将此文本拆分为空白,则会获得带有x
元素的子串(z)数组。
我认为您的代码需要输入z.length
,并且应该在y [0]中添加2,7,1,2,在[1]和6,5,7中添加5,6,8,3, y [2]中的9;
您将获得的这些问题: